Common mistakes (and how to avoid)


  • I would like to talk about some common mistakes I've seen.

    Common mistake #1:

    Declaring variables to properties

    --LocalScript, inside a TextLabel
    local text = script.Parent.Text
    
    text = "hi how r u kbye"
    

    The problem lies in the fact that the user wrongly assumes that thetext variable is a reference to the actual property of theTextLabel. If the text of the TextLabel changes, the variables you have defined won't update; only its Text property will change. In order to fix this, you can remove the text variable and only make direct accesses to the Text property of the TextLabel. You could also modify the variables to hold theTextLabel object itself. So if the current text was "qwerty", line 2 of my script would be the same as:

    local text = "qwerty" 
    

    Now, on line 4, where many users complain that their text isn't changing, they are actually just reassigning the text variable.

    --LocalScript, inside a TextLabel
    local text = script.Parent
    
    text.Text = "hi how r u kbye"
    

    We have functioning code. The same happens for ValueBase objects, but again, assign it to the object,
    not .Value.

    Common mistake #2

    Conditional statements checking if a value equals something

    What I mean, is something like this:

    local myString = "subscribe"
    
    if myString == "subscribe" or "hello" then
        
    end
    

    What they're doing, is checking if myString equals "subscribe", which is correct, but on or "hello", is where they're making a mistake. What they are doing now, is checking if the string equals "subscribe", and also checking if the string "hello" exists, or if the string is true, not if myString equals "hello". Values that are not nil / not false in Lua are considered truthy, so that line above would be the same as:

    if myString == "subscribe" or true then

    To fix this:

    if myString == "subscribe" or myString == "hello" then
        -- Code
    end
    

    Working code :)

    Common mistake #3

    Adding a player parameter to OnClientInvoke/OnClientEvent

    This is a common mistake with RemoteEvents and RemoteFunctions. Many users may know that the first parameter to OnServerEvent and OnServerInvoke is always the Player that fired/invoked the remote, or because the first argument to Invoke/FireClient is always the Player object to fire/invoke to. They pass it on to OnClient, but is not necessary, since the client doesn't need to know who fired the remote. The server is firing the remote to the client.

    --LocalScript
    RE.OnClientEvent:Connect(function(plr, arg1, arg2)
        --Code 
    end)
    

    From the server:

    RE:FireClient(player, arg1, arg2)
    

    That line will error. Why? In the OnClientEvent, arg1 was not meant to be a Player object. To fix that, make it RE:FireClient(player, player, arg1, arg2). But two player arguments look really bad, so just remove plr on my OnClientEvent

    RE.OnClientEvent:Connect(function(arg1, arg2)
    
    end)
    

    Now from the server:
    RE:FireClient(plr, arg1, arg2)

    It works now!!! If you want me to add another common mistake, tell me and I will edit my post to add it. You will be credited.


  • If doing #2 alot, this function may be helpful:

    local function ifOrState(var, ...)
    	local pass = false
    	for i, v in pairs({...}) do
    		if var == v then pass = true end
    	end
    	return pass
    end
    
    --example of use
    
    local str = "hi"
    
    print(ifOrState(str, "hello", "hi", "bye")) -->> prints 'true', shortens code
    
Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.