Guide To Debugging Common Problems


  • Premise of the Guide

    Hello everyone, due to the presence of many many questions, either on discord, on on the site, I feel that it would be prudent to create a guide on debugging these issues.

    If your question, or a question you're answer has one of the following issues, make sure to link this page, or link
    this
    :wink:

    LocalPlayer is a nil value

    The reason this is an issue is because you are using game.Players.LocalPlayer in a Normal/Server script, this is a BIG no-no. The server, especially one with >1 player, has no concept of a local player There are no discerning characteristics one player that makes them more normal/local than another player.

    At this point, you may be asking: "Well how do I get a player in a server script"

    The answer to that depends on what you are trying to do.

    If you want to see if a certain player touched a part, you can do the following:

    part.Touched:Connect(function(hit)
        local plr = game:GetPlayerFromCharacter(hit.Parent)
        if plr then
            ...
        end
    end)
    

    If you want to get a player from a click detector , you can do this:

    ClickDetector.MouseClick:Connect(function(plr)
        ...
    end)
    

    If you want to get all players, you can do this

    for _,plr in ipairs(game.Players:GetPlayers()) do
        ...
    end)
    

    Unable to Cast Value to Object

    If this is your issue, The most Probable cause is that You aren't using the Value property of a ValueObject, and instead just using the object for arithmetic, setting values, etc.

    For example:

    local thingy = workspace.IntValue
    local Thingy2 = workspace.NumberValue
    Thingy2.Value = thingy --error unable to cast value to object
    

    The fix would be to put a .Value when using the reference

    local thingy = workspace.IntValue
    local Thingy2 = workspace.NumberValue
    Thingy2.Value = thingy.Value 
    

    Gui doesnt change when you modify it in StarterGui

    If this is a Server Script, stop. And change it to a localscript, this is because GUI objects such as "Frames" or "TextLabels" clone locally from the StarterGui into what you should be using, the PlayerGui, which is a child of The Player.

    If this is a local script, simply change the reference to the StarterGui to a reference of the PlayerGui

    Change

    local plr = game.Players.LocalPlayer
    local starterGui = game.StarterGui
    --blah blah blah
    

    to

    local plr = game.Players.LocalPlayer
    local starterGui = plr:WaitForChild("PlayerGui")
    --blah blah blah
    

    Script Doesn't run when Disabled is False

    If this is a server script, you probably have it somewhere where they can't run, such as:

    • The Client
    • Replicated Storage
    • Lighting
    • Server Storage

    For Localscripts, the premise holds, however, the locations change a bit

    • Anywhere on the server
    • Lighting
    • Replicated Storage

    Attempted To perform Arithmetic on a UserData

    This probably means that you tried to add, subtract, divide, etc. a Object (what essentially a userdata is).
    This generally is a Common problem when using Value objects, especially NumberValues and IntValues
    This, like the answer to Unable to Cast Value to Object is simple, simply add a .Value

    local thingy = workspace.IntValue
    local Thingy2 = workspace.NumberValue
    print(Thingy2.Value + thingy)
    

    To:

    local thingy = workspace.IntValue
    local Thingy2 = workspace.NumberValue
    print(Thingy2.Value + thingy.Value) 
    

    bad argument #1/#2 to 'random' (interval is empty)

    bad argument #1 to 'random' (interval is empty):

    This means that you're first argument provided is smaller than 1, to fix it, you would have to give it an integer greater than or equal to 1

    print(math.random(21))
    

    bad argument #2 to 'random' (interval is empty):

    This means that you're 2nd argument provided is smaller than the first, to fix it, you would have to give it an integer greater than or equal to the first

    print(math.random(21,42))
    

    Unknown Global x

    Basically, it means that variable x is undefined, an example of that in lua 5.1 would be

    function Foo(...)
        for k, v in pairs(arg) do
            print(v)
        end
    end
    
    Foo("Hello", "World!", "", "How", "Are", "You", "Today")
    

    In that example, variable arg isnt defined, thus erroring that.
    However, something like this would fix the issue

    local function foo(...)
        local args = {...};
    
        for _, arg in next, args do
            -- Blah
        end 
    end
    

    Conclusions

    Hopefully this helped some of you with your problems, and if there are any suggestions you have or inaccuracies in my examples, make sure to let me know :smile:


  • @theking48989987
    >Local<Player
    >Local<Script
    Its in the name, people.


  • @TheeDeathCaster ehhh, at least 21 people have failed to make that connection


  • @theking48989987 cool article!

Log in to reply
 

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