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

Attempt to index global error?

I used a code that move model that i named 'ladder'

Part = script.Parent
Model = game.Workspace:WaitForChild("Ladder")

Part.ClickDetector.MouseClick:connect(function()
	Ladder:MoveTo(Vector3.new(177.331, 22.626, 100.366))
end)

This is the code, but i get

Workspace.Part.Script:5: attempt to index global 'Ladder' (a nil value)

error, so what is the problem?

3 comments

0

Model -> Ladder

0

you never defined a Ladder variable, instead, you defined a "Model" variable

0

Thanks

1 answer

✓ Accepted answer

Your error occured because you set the variable name of your ladder to Model in line 2, but you tried to call it as Ladder in line 5.

Also, it is better to use local variables as they can be accessed faster than non-local variables.

local Part = script.Parent
local Model = game.Workspace:WaitForChild("Ladder")

Part.ClickDetector.MouseClick:Connect(function()
    Model:MoveTo(Vector3.new(177.331, 22.626, 100.366))
end)

4 comments

0

Thanks, it works now

0

It's not technically a global variable. Global would be _G.

0

However, I do agree with you, and locally defined variables are generally faster and better practice.

0

I changed it into non-local if it's more correct :)

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