Respawning to wrong stage in obby then dying

im unsure how i’d combine the scripts.

Heres the script from the video:


local checkpoints = workspace:WaitForChild("Checkpoints")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local stage = Instance.new("IntValue")
	stage.Name = "Stage"	
	stage.Value = 1
	stage.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		wait()
		char:MoveTo(checkpoints[stage.Value].Position)
		
		hum.Touched:Connect(function(hit)
			if hit.Parent == checkpoints then
				if tonumber(hit.Name) == stage.Value + 1 then
					stage.Value = stage.Value + 1
				end
				
			end

			
			
		end)
		
		
		
	end)
	
	
	
end)

and heres the data store script:

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 * CFrame.new(0,10,0)

			else
				local part = workspace.Checkpoints.spawn
				hrp.CFrame = part.CFrame * CFrame.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)
1 Like
local checkpoints = workspace:WaitForChild("Checkpoints")
local Datastoreservice = game:GetService(“DataStoreService”)
local ds = Datastoreservice:GetDataStore(“PlayerData”)

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

    local data = ds:GetAsync(player.UserId)

    if data then
        stage.Value = data
    else
        stage.Value = 1
    end
	
	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		wait()
		char:MoveTo(checkpoints[stage.Value].Position)
		
		hum.Touched:Connect(function(hit)
			if hit.Parent == checkpoints then
				if tonumber(hit.Name) == stage.Value + 1 then
					stage.Value = stage.Value + 1
				end
			end
		end)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    local id = player.UserId

    local success, err = pcall(function()
        ds:SetAsync(id, player.leaderstats.Stage.Value)
    end)
end)

game:BindToClose:Connect(function()
    for i, player in pairs(game.Players:GetChildren()) do
        local id = player.UserId

        local success, err = pcall(function()
            ds:SetAsync(id, player.leaderstats.Stage.Value)
        end)
    end
end)

Let me know if this works :slight_smile:

1 Like

Yes this worked thankyou so much!!! :slight_smile:

No problem. :slight_smile:

I didn’t add game:BindToClose though, so I will add that shortly to the script…

okieee adding that now thanyou!