Checkpoint isn't saving correctly

My checkpoint script isn’t working, I don’t know what I am doing incorrect.

I made a simple checkpoint script, but when I die it does not respawn me back to my checkpoint, it resets it back to the default spawn. Everything in the script seemed correct

https://gyazo.com/f083cae3f8b6d3c6a2d4cd374052fc16

Script in serverscriptservice:

local Players = game:GetService("Players")
local checkPoints = workspace:WaitForChild("oc")
local defaultSpawn = checkPoints:WaitForChild("1")


game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
	local HRP = character:WaitForChild("HumanoidRootPart")
	HRP.CFrame = defaultSpawn.CFrame * CFrame.new(0,1,0)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local Stage = Instance.new("IntValue")
	Stage.Name = "Stage"
	Stage.Value = 1
	Stage.Parent = leaderstats
	end)
end)

Script under the spawnPoint

local checkPoint = script.Parent
local checkPointNum = checkPoint:WaitForChild("CheckpointNum").Value

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if hit.Parent.Humanoid.Health > 0 then
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			local HRP = player.Character:WaitForChild("HumanoidRootPart")
			
			if player.leaderstats.Stage.Value < checkPointNum then
				print("Player"..player.Name.."Has reached level"..checkPointNum)
				player.leaderstats.Stage.Value = checkPointNum
				HRP.CFrame = checkPoint.CFrame * CFrame.new(0,1,0)
			end
		end
	end
end)

You’re creating a new leaderstats folder everytime the player character respawn

Put the entire leaderstat code out of the CharacterAdded event like this:

To update the default spawn, you can do something like this:

local Players = game:GetService("Players")
local checkPoints = workspace:WaitForChild("oc")
local defaultSpawn = checkPoints:WaitForChild("1")


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local Stage = Instance.new("IntValue")
	Stage.Name = "Stage"
	Stage.Value = 1
	Stage.Parent = leaderstats

	Stage.Changed:Connect(function()
		defaultSpawn = newStageSpawn -- put your new stage spawn part here
	end)

	player.CharacterAdded:Connect(function(character)
	local HRP = character:WaitForChild("HumanoidRootPart")
	HRP.CFrame = defaultSpawn.CFrame * CFrame.new(0,1,0)
	end)
end)
1 Like

That worked and update the IntValue but the spawn location is still being set the to the defaultspawn.

https://gyazo.com/bc80dbcb0d7970406a6489a48089e048

Oh, because in CharacterAdded you are making the player HRP go to the defaultSpawn.CFrame

When I try that it spawns me above the zone which is strange

Updated coded:

local Players = game:GetService("Players")
local checkPoints = workspace:WaitForChild("oc")
local defaultSpawn = checkPoints:WaitForChild("1")


game.Players.PlayerAdded:Connect(function(player)
	player.RespawnLocation = defaultSpawn
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local Stage = Instance.new("IntValue")
	Stage.Name = "Stage"
	Stage.Value = 1
	Stage.Parent = leaderstats
end)

What I am getting:
https://gyazo.com/83ab08160fa131ff0fc97a1907e2cafb

Uhhh, don’t use player.RespawnLocation :sob:

Can you tell how your checkpoints are named? Like is the first checkpoint is named “1” and so forth?

Put something like this in your PlayerAdded event

	Stage.Changed:Connect(function()
		defaultSpawn = newStageSpawn -- put your new stage spawn part here
	end)

Yes, it is named 1 and so forth

What do you mean? By creating a new part?

Perfect! You can do:

	Stage.Changed:Connect(function()
		defaultSpawn = checkPoints:WaitForChild(Stage.Value) -- get your new checkpoint part
	end)

You can ignore that, just look at this one above

New code would be:

local Players = game:GetService("Players")
local checkPoints = workspace:WaitForChild("oc")
local defaultSpawn = checkPoints:WaitForChild("1")


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local Stage = Instance.new("IntValue")
	Stage.Name = "Stage"
	Stage.Value = 1
	Stage.Parent = leaderstats

	Stage.Changed:Connect(function() -- this fires every time your stage value in leaderstats get changed
		defaultSpawn = checkPoints:WaitForChild(Stage.Value)
	end)

	player.CharacterAdded:Connect(function(character)
	local HRP = character:WaitForChild("HumanoidRootPart")
	HRP.CFrame = defaultSpawn.CFrame * CFrame.new(0,1,0)
	end)
end)
``
1 Like

The reason this doesn’t work is because everytime the character spawns, you’re setting the stage to 1 every time.

1 Like
local Players = game:GetService("Players")
local checkPoints = workspace:WaitForChild("oc")

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

	local Stage = Instance.new("IntValue")
	Stage.Name = "Stage"
	Stage.Value = 1
	Stage.Parent = leaderstats

    leaderstats.Parent = player

	player.CharacterAdded:Connect(function(character)
        character:PivotTo(checkPoints:WaitForChild(tostring(Stage.Value)):GetPivot() * CFrame.new(0, 1, 0))
	end)
end)
local checkPoint = script.Parent
local checkPointNum = checkPoint:WaitForChild("CheckpointNum").Value

checkPoint.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if hit.Parent.Humanoid.Health > 0 then
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            if not player then return end
			
			if player.leaderstats.Stage.Value < checkPointNum then
				print(string.format("Player %s has reached level %i!", player.Name, checkPointNum))
				player.leaderstats.Stage.Value = checkPointNum
				hit.Parent:PivotTo(checkPoint:GetPivot() * CFrame.new(0,1,0))
			end
		end
	end
end)
1 Like

Both solutions have worked, thank you for the help.

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