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

Need Help With Global Variables [?]

Asked by 8 years ago

These are the codes:

function Change()
_G.Checkpoint = script.Parent
end

script.Parent.Touched:connect(Change)

Next one:

function onPlayerEntered(newPlayer)
newPlayer:WaitForChild("Torso").CFrame = _G.Checkpoint.CFrame
end
game.Players.ChildAdded:connect(onPlayerEntered)

function onPlayerRespawned(newPlayer) 
h = newPlayer:WaitForChild("Torso")
if h ~= nil then
h.CFrame = _G.Checkpoint.CFrame
end
end
game.Workspace.ChildAdded:connect(onPlayerRespawned)

I'm trying to make a non-leaderstat checkpoint system, like if I were to just copy the same block over and over, the checkpoints would still work.

Errors:

Workspace.Script:9: Attempt to index field "Checkpoint" (a nil value)

1 answer

Log in to vote
0
Answered by 8 years ago

Unlike local Variable, global Variable takes time to load so I suggest added a wait before line 9 to make sure the script doesn't run before the global variable has loaded.

function onPlayerEntered(newPlayer)
newPlayer:WaitForChild("Torso").CFrame = _G.Checkpoint.CFrame
end
game.Players.ChildAdded:connect(onPlayerEntered)

function onPlayerRespawned(newPlayer) 
h = newPlayer:WaitForChild("Torso")
if h ~= nil then
wait(2)
h.CFrame = _G.Checkpoint.CFrame
end
end
game.Workspace.ChildAdded:connect(onPlayerRespawned)

0
Yay it works! connor12260311 383 — 8y
Ad

Answer this question