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

Need Help With Global Variables [?]

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

✓ Accepted answer

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)

1 comment

0

Yay it works!

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