Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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 comment

1

ew. global variables

1 answer

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.