When a player dies in my obby their stage leaderstat gets reset to 0. They still respawn at the right checkpoint, but their stage number visually says 0 in the leaderboard until they reach the next checkpoint. I have pasted every script I have relating to the checkpoint system; some might not be necessary.
Leaderstats script in ServerScriptService:
game.Players.PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(char) local ls = Instance.new("Folder", player) ls.Name = "leaderstats" local stage = Instance.new("NumberValue", ls) stage.Value = 0 stage.Name = "Stage" local stageJoined = player.leaderstats.stage.Value wait(0.01) char.HumanoidRootPart.Position = game.workspace.Stages2:FindFirstChild("Stage"..stageJoined).Position end) end)
Checkpoints script in ServerScriptService:
local Players = game:GetService('Players') Players.PlayerAdded:Connect(function(player) local stage = player:WaitForChild("leaderstats"):WaitForChild('Stage') player.CharacterAdded:Connect(function(character) local checkpoint = game.Workspace.Stages:FindFirstChild(stage.Value) delay(.01, function() character:SetPrimaryPartCFrame(checkpoint.CFrame + Vector3.new(0,10,0)) end) end) end)
CheckpointManager Script in the "Stages" folder:
for i,v in pairs(script.Parent:GetChildren()) do if v:isA("Part") then v.Touched:Connect(function(hit) local character = hit.Parent local player = game.Players:GetPlayerFromCharacter(character) if player then local touchedstage = tonumber(v.Name) if touchedstage == (player.leaderstats.Stage.Value + 1) then player.leaderstats.Stage.Value = touchedstage end end end) end end
Datastore script in ServerScriptService:
local DSService = game:GetService('DataStoreService'):GetDataStore('PUTRANDOMCHARACTERSHERE') game.Players.PlayerAdded:connect(function(plr) local uniquekey = 'id-'..plr.userId local leaderstats = Instance.new('IntValue', plr) local savevalue = Instance.new('IntValue') leaderstats.Name = 'leaderstats' savevalue.Parent = leaderstats savevalue.Name = 'Stage' local GetSaved = DSService:GetAsync(uniquekey) if GetSaved then savevalue.Value = GetSaved[1] else local NumbersForSaving = {savevalue.Value} DSService:SetAsync(uniquekey, NumbersForSaving) end end) game.Players.PlayerRemoving:connect(function(plr) local uniquekey = 'id-'..plr.userId local Savetable = {plr.leaderstats.Stage.Value} DSService:SetAsync(uniquekey, Savetable) end)
at leaderStats scripts line 2 it states that when the player respawns you create a new leaderstats https://developer.roblox.com/en-us/api-reference/event/Player/CharacterAdded "The CharacterAdded event fires when a player’s character spawns (or respawns)" also it might be better for performance to not use instance.new() for parenting
game.Players.PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(char) local ls = Instance.new("Folder", player) ls.Name = "leaderstats" local stage = Instance.new("NumberValue", ls) stage.Value = 0 stage.Name = "Stage" local stageJoined = player.leaderstats.stage.Value wait(0.01) char.HumanoidRootPart.Position = game.workspace.Stages2:FindFirstChild("Stage"..stageJoined).Position end) end)