Respawning to wrong stage in obby then dying

Hi, whenever i die in my obby, i get respawned to the wrong level then instantly killed until i end up spawning to my current stage. I genuinely dont know why it is doing this and ive looked over my code and not sure where to start.

Heres my code for saving the stages:

local players = game:GetService("Players")
local datastoreservice = game:GetService("DataStoreService")
local saveDataStore = datastoreservice:GetDataStore("SaveDataTest")


local function SavePlrData(plr)
	
	local success,err = pcall(function()
		
	
	
	local saveData = {}
	
	for _,stat in pairs(plr.leaderstats:GetChildren()) do
			saveData[stat.Name] = stat.Value
			
		end
		
		saveDataStore:SetAsync(plr.UserId,saveData)
		
	end)
	
	if not success then return err end
	
end
players.PlayerAdded:Connect(function(plr)
	
	local stats = Instance.new("Folder")
	
	stats.Name = "leaderstats"
	stats.Parent = plr
	
	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Parent = stats
	
	local data = saveDataStore:GetAsync(plr.UserId)
	
	if data then
		
		
		print(data.Stage)
		
		
		for _,stat in pairs(stats:GetChildren()) do
			stat.Value = data[stat.Name]
		end
		
	else
		print(plr.Name.. "has no data")
	end
	
	
	
	plr.CharacterAdded:Connect(function(char)
		local humanoid,hrp = char:WaitForChild("Humanoid"), char:WaitForChild("HumanoidRootPart")
		
		wait()
		
		if humanoid and hrp then
			if stage.Value ~= 0 then
				local part = workspace.Checkpoints:FindFirstChild(stage.Value)
				hrp.CFrame = part.CFrame + Vector3.new(0,10,0)
				
			
				local part = workspace.Checkpoints.spawn
				hrp.CFrame = part.CFrame + Vector3.new(0,10,0)
			end
		end
	end)
	
end)



players.PlayerRemoving:Connect(function(plr)
	
	local err = SavePlrData(plr)
	
	if err then print(err) end
	
	
end)


game:BindToClose(function()
	
	for _,plr in pairs(players:GetPlayers()) do	local err = SavePlrData(plr)

		if err then print(err) end
		end
	
	wait(2)
	
end)

and heres my code for the ‘when player touches checkpoint’

local obbystages = workspace:WaitForChild("Checkpoints")

for _,stage in pairs(obbystages:GetChildren()) do
	
	stage.Touched:Connect(function(hit)
		local hum
		if hit.Parent:FindFirstChild("Humanoid") then
			
			hum = hit.Parent.Humanoid
		end
		
		if hit.Parent and hit.Parent.Parent:FindFirstChild("Humanoid") then
			hum = hit.Parent.Parent.Humanoid
		end
		
		if hum then
			local plr = game.Players:GetPlayerFromCharacter(hum.Parent)
			
			local plrstage = plr.leaderstats.Stage.Value
			
			if tonumber(stage.Name) == plrstage + 1 then
				plr.leaderstats.Stage.Value = plr.leaderstats.Stage.Value + 1
				
			elseif tonumber (stage.Name) > plrstage + 1 then
				hum.Health = 0
			end
			
		end
		
	end)
end
2 Likes

you did not set the players position the the checkpoint when he dies…

try

Humanoid.Died

then set his position to the stage where he take his checkpoint where is

obbyStages:FindFirstChild(leaderstats.Stage.Value)

What does these lines of code do?

local part = workspace.Checkpoints.spawn
				hrp.CFrame = part.CFrame + Vector3.new(0,10,0)

It looks like after you teleport them to their checkpoint, you teleport them back to the spawn. Did you mean to put that outside of the if statement?

yeah i think so but im honestly not sure

where would i insert this in my code?

put else just 1 line upper

1 Like

sorry, i misunderstanded the code, nevermind that answer

yes just done that realise i deleted it earlier lol. I still have the same issue with the script tho

Use this bit of code instead:

if humanoid and hrp then
			if stage.Value ~= 0 then
				local part = workspace.Checkpoints:FindFirstChild(stage.Value)
				hrp.CFrame = part.CFrame + Vector3.new(0,10,0)
				
			else
				local part = workspace.Checkpoints.spawn
				hrp.CFrame = part.CFrame + Vector3.new(0,10,0)
			end
		end

will this fix the character dying bug

if that doesnt work too, try this

            if stage.Value ~= 0 then
				local part = workspace.Checkpoints:FindFirstChild(stage.Value)
				hrp.CFrame = part.CFrame * CFrame.new(0,10,0)
				
			else
				local part = workspace.Checkpoints.spawn
				hrp.CFrame = part.CFrame * CFrame.new(0,10,0)
			end
1 Like

this didnt fix it either. i just keep dying

you die, then it spawns you in wrong place ?

The reason it keeps killing you is because when you respawn at the beginning, it is not equal to your stage value + 1, so it kills you.

So yeah it should fix it :slight_smile:

look @Carl_Weezer13’s Response

the code didnt work i dont think because i still keep dying

Do you think you could include your leaderboard in the video so I could see what is happening with that?

You could refer to this video on how to create checkpoints:

Just combine this with your datastore script, to have the checkpoints and datastore in one script