Checkpoint Script Help

Hey everyone, I’m currently having issues with my checkpoint script. The script takes a value from the player and uses that to decide where they spawn. Currently the script is working fully but occassionally the player spawns the player off in the void and they fall to their death. This happens about a 5th of the time and I’m stumped as to why. Attached is a video of the player respawning normally a few times then spawning somewhere random and dying.

robloxapp-20230524-0115388.wmv (2.8 MB)

The script in question

function plrToStage(plr)
	repeat wait() until plr.Character.HumanoidRootPart

	local stagePart = game.Workspace.Checkpoints:FindFirstChild(tostring(plr.Values.Stage.Value))

	plr.Character.HumanoidRootPart.CFrame = stagePart.CFrame + Vector3.new(0,2.5,0)
end

game.Players.PlayerAdded:Connect(function(player)
	if player.Character then
		plrToStage(player)
	end

	player.CharacterAdded:Connect(function()
		plrToStage(player)
	end)
end)

for _, cheackpoint in pairs(game.Workspace.Checkpoints:GetChildren()) do
	cheackpoint.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)

			local cheackpointNumber = tonumber(cheackpoint.Name)

			player.Values.Stage.Value = cheackpointNumber
		end
	end)
end

Originally this script was apart of my leaderboard that tracks and assigns stats to the player but I moved it to a seperate script to see if the leaderboard script was causing the checkpoint portion of the script to lag behind. This didn’t fix the issue and now I’m stumped and could use a hand.

pretty sure it has to do with the game’s player loading stuff. it teleports the player to a designated spawn location after the player is loaded, so it may be running after your code and overriding it

you can try using Player.CharacterLoaded:Wait() to wait for the player to load instead of repeat wait() until plr.Character.HumanoidRootPart (or Player.CharacterAppearanceLoaded:Wait() to wait for it to fully load (more safe)) and then pause by a frame (game:GetService('RunService).Stepped:Wait()) and this should help.

here is some reading on a similar problem (i reccomend searching for existing answers before posting your own)

3 Likes

Hey,

Thanks for the quick reply, I swapped out repeat wait() until plr.Character.HumanoidRootPart for Player.CharacterAppearanceLoaded:Wait(), (`game:GetService('RunService).Stepped:Wait() and now the script is running smoothly. Thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.