Why does the checkpoint not work the first time?

script:

local StageData = game:GetService("DataStoreService"):GetDataStore("StageData")


local Checkpoints = workspace:WaitForChild("Checkpoints"):GetChildren()

function GoToCheckpoint(character, stage)
	local rootPart = character:WaitForChild("HumanoidRootPart")
	repeat wait(0.00) until rootPart

	for i, checkpoint in pairs(Checkpoints) do
		print("Checkpoint" .. tostring(stage))
		if ("Checkpoint" .. tostring(stage)) == checkpoint.Name then
			print("Checkpoint found")
			rootPart.CFrame = checkpoint.CFrame * CFrame.new(0,1,0)
	
			break
			-- Stop looping since we found the checkpoint
		end
	end
end

game:GetService("Players").PlayerAdded:Connect(function(player)

	wait(0.1)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local stage = Instance.new("IntValue", leaderstats)
	stage.Name = "Stage"
	stage.Value = StageData:GetAsync(player.UserId) or 1
	-- Retrieve the player's data, but if there is none, start the player on stage 1

	player.CharacterAdded:Connect(function(character)

		wait(0.1)
		-- The function we're connecting to this event will be called everytime the player's character spawns
		GoToCheckpoint(character, stage.Value)
	end)
end)

for i, checkpoint in pairs(Checkpoints) do
	checkpoint.Touched:Connect(function(touch)
		-- Touched is an event that is fired when the object is touched
		local hum = touch:FindFirstChild("Plr")
		-- If a part in a model touches the part, touch = that part not the model
		if hum then

			-- The thing that touched the checkpoint is alive

			-- Whatever touched the checkpoint is a player
			local leaderstats = game.Players[hum.Value]:WaitForChild("leaderstats")
			local stage = leaderstats:WaitForChild("Stage")
			-- Find the stage leader stat
			if (tonumber(checkpoint.Name:match("%d+"))-stage.Value) == 1 then
				-- match() finds certain parts in a string
				-- So when we do match("%d"), we're finding the numbers in a string
				-- This is the player's next checkpoint
				-- This makes it so player's can't skip checkpoints
				-- or lose progress if they accidently go backwards
				stage.Value = stage.Value + 1

			end
		end
	end)
end

game:GetService("Players").PlayerRemoving:Connect(function(player)
	StageData:SetAsync(player.UserId, player.leaderstats.Stage.Value)
	-- Saves the player's data when they leave the game
end)

when i spawn in, i get spawned in the void, and when i died the checkpoint registers and i get spawned in like normal.

3 Likes

Easy fix, all you have to do is make it so the GoToCheckpoint function is run before calling the character-added connection. The reason is that by the time your code gets to that point the character is already spawned meaning the function won’t trigger meaning you won’t be sent to the checkpoint.

Hope this helps

2 Likes

but won’t the character not exist? there will be nothing to teleport

2 Likes

i would use this

workspace:WaitForChild(player.Name)

to get the players character to make sure it exists and if it doesn’t wait for it

2 Likes

oddly, it did the opposite, the first time worked and all the times after than is just me going into the void

1 Like
ame:GetService("Players").PlayerAdded:Connect(function(player)

	wait(0.1)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local stage = Instance.new("IntValue", leaderstats)
	stage.Name = "Stage"
	stage.Value = StageData:GetAsync(player.UserId) or 1
	-- Retrieve the player's data, but if there is none, start the player on stage 1
	local character = workspace:WaitForChild(player.Name)
	GoToCheckpoint(character, stage.Value)
	--player.CharacterAdded:Connect(function(character)
		
	--	wait(0.1)
	--	-- The function we're connecting to this event will be called everytime the player's character spawns
		
	--end)
end)
1 Like

You still need the other character added code you just needed the new code to Initialize that process before it could trigger

do this

game:GetService("Players").PlayerAdded:Connect(function(player)
	wait(0.1)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local stage = Instance.new("IntValue", leaderstats)
	stage.Name = "Stage"
	stage.Value = StageData:GetAsync(player.UserId) or 1
	-- Retrieve the player's data, but if there is none, start the player on stage 1
	local character = workspace:WaitForChild(player.Name)
	GoToCheckpoint(character, stage.Value)

	player.CharacterAdded:Connect(function(character)
		wait(0.1)
		-- The function we're connecting to this event will be called everytime the player's character spawns
	end)
end)

nope, nothing changed, still a sad fate of the void

oh i see the function is missing, here try this

game:GetService("Players").PlayerAdded:Connect(function(player)
	wait(0.1)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local stage = Instance.new("IntValue", leaderstats)
	stage.Name = "Stage"
	stage.Value = StageData:GetAsync(player.UserId) or 1
	-- Retrieve the player's data, but if there is none, start the player on stage 1
	local character = workspace:WaitForChild(player.Name)
	GoToCheckpoint(character, stage.Value)

	player.CharacterAdded:Connect(function(character)
		wait(0.1)
        GoToCheckpoint(character, stage.Value)
		-- The function we're connecting to this event will be called everytime the player's character spawns
	end)
end)

odd solution but it worked, thanks a lot

np, another way that this could have worked is running the character added before any of the other code within the player added function

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