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

Attempt to index global error?

Asked by 5 years ago

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?

0
Model -> Ladder GoldAngelInDisguise 297 — 5y
0
you never defined a Ladder variable, instead, you defined a "Model" variable theking48989987 2147 — 5y
0
Thanks WhyICreatedThiz -3 — 5y

1 answer

Log in to vote
0
Answered by
Rheines 661 Moderation Voter
5 years ago
Edited 5 years ago

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)
0
Thanks, it works now WhyICreatedThiz -3 — 5y
0
It's not technically a global variable. Global would be _G. User#25115 0 — 5y
0
However, I do agree with you, and locally defined variables are generally faster and better practice. User#25115 0 — 5y
0
I changed it into non-local if it's more correct :) Rheines 661 — 5y
Ad

Answer this question