Problem with Checkpoints

Hey Developers,

In my game I added checkpoint spawns they are working but if I leave the the server and rejoin in the same server the check points does NOT work.

Can you help me?

Here is the script inside the Checkpoint spawns:

spawn.Touched:connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local checkpointData = game.ServerStorage:FindFirstChild("CheckpointData")
		if not checkpointData then
			checkpointData = Instance.new("Model", game.ServerStorage)
			checkpointData.Name = "CheckpointData"
		end
		
		local checkpoint = checkpointData:FindFirstChild(tostring(player.userId))
		if not checkpoint then
			checkpoint = Instance.new("ObjectValue", checkpointData)
			checkpoint.Name = tostring(player.userId)
			
			player.CharacterAdded:connect(function(character)
				wait()
				character:WaitForChild("HumanoidRootPart").CFrame = game.ServerStorage.CheckpointData[tostring(player.userId)].Value.CFrame + Vector3.new(0, 4, 0)
			end)
		end
		
		checkpoint.Value = spawn
	end
end)
2 Likes

If that player.CharacterAdded event is the only thing teleporting players, then that is probably your problem. If you rejoined and the “checkpoint” from checkpointData already existed, then the CharacterAdded event wont fire.

Also, I’m not really sure if doing that player.CharacterAdded in a touched event is that good of an idea…

so do I have to make it repeated or something like this?

You need to add datastores.
Add this into a serverscript:

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("Checkpoints")

local defaultCheckpoint = 0

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

    local cp = Instance.new("IntValue")
    cp.Name = "Checkpoint"
    cp.Value = defaultCheckpoint

    pcall(function()
        cp.Value = ds:GetAsync("Player_"..player.UserId)
    end)

    cp.Parent = ls
    ls.Parent = player
end)

game.Players.PlayerRemoving:Connect(function(player)
    ds:SetAsync("Player_"..player.UserId, player.leaderstats.Checkpoint.Value)
end)

this should be another server script in serverscriptservice:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character:PivotTo(workspace.Spawns:FindFirstChild(tostring(player:WaitForChild("leaderstats"):WaitForChild("Checkpoint").Value)):GetPivot())
    end)
end)

and to modify your script:

script.Parent.Touched:Connect(function(hit)
    if hit and hit.Parent:FindFirstChildWhichIsA("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            player.leaderstats.Checkpoint.Value = tonumber(script.Parent.Name)
        end
    end
end)

obviously, this isn’t the best or even good at all
but it works

hell, I typed this all on mobile :confused:

1 Like

I am sorry for you typing this on mobile for nothing it didn’t work :frowning:

Of course it didn’t work, you need to modify some things.
What are the errors?

No errors Nothing showed what do i need to modify?