Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

why i'm getting the error "attempt to index nil with "set""?

Asked by 4 years ago

The error happens in the _G.Vehicle Health, i also have another command that is is connected to this script its on the bottom

    local hum = tie:FindFirstChild'Humanoid'
    if hum and hum.Health/hum.MaxHealth <= .4 then
        script.Alarm.Volume = 1
    else
        script.Alarm.Volume = 0
    end
    _G.VehicleHealth.Set(hum.Health/hum.MaxHealth)
end)

the script on the StarterGui

function _G.VehicleHealth.Set(n)
    script.Parent.Parent.Visible = true
    ChangeHealth(n,1)
end

1
ew. global variables greatneil80 2647 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You mention that the script setting the variable is in StarterGui, so it must be a LocalScript (If it isn't, then it isn't running so the variable wouldn't get set anyway).

I also am assuming that first script is not a LocalScript, and so _G.VehicleHealth.Set doesn't exist yet on the server. The _G table is shared only between scripts in the same context level, so the server does not have access to the same _G table as the client (And therefore errors when you try to index it with non-existent keys). To fix this you need to be setting the function in _G in the same context level as where you're calling it.

Ad

Answer this question