I'm doing a checkpoint system that saves every time the player gets a new checkpoint. The problem is that the player doesn't spawn on the checkpoint,and every time the player leaves and go back to the map the leaderstats go back to 1,even if i change the or to 0. the checkpoint script that is on server scripts.
local DS = game:GetService("DataStoreService") local PD = DS:GetDataStore("PD") game.Players.PlayerAdded:Connect(function(plr) local leaderstats= Instance.new("Folder", plr) leaderstats.Name = "leaderstats" local Stage = Instance.new("IntValue", leaderstats) Stage.Name= "Stage" Stage.Value = PD:GetAsync(plr.UserId) or 0 PD:SetAsync(plr.UserId, Stage.Value) Stage.Changed:Connect(function() local success, Error = pcall(function() PD:SetAsync(plr.UserId .. "Stage", plr.leaderstats.Stage.Value) end) if success then print("data saved") else print("error 2 ") warn(Error) end end) end) function checkspawn(players) local plr=game.Players:GetPlayerFromCharacter(players) if plr~=nil then local check=plr.leaderstats local value = game.workspace:FindFirstChild(check.Stage.Value) print("obby") players.HumanoidRootPart.CFrame =players.HumanoidRootPart.CFrame + Vector3.new(0,3,0) wait() players.HumanoidRootPart.CFrame = value.CFrame + Vector3.new(0,3,0) end end game.Players.ChildAdded:Connect(checkspawn)
the script on the checkpoints
function ot(hit) if hit.Parent ~= nil then local players = game.Players:playerFromCharacter(hit.Parent) if players ~= nil then if players.leaderstats.Stage.Value == script.Parent.Name - 1 then local h = hit.Parent:FindFirstChild("Humanoid") if h ~= nil then if h.Health ~= 0 then players.leaderstats.Stage.Value = script.Parent.Name end end end end end end script.Parent.Touched:connect(ot)
From what I can figure out, changing the or 0
to or 2
shouldn't make it go to 1. If that's the case, then the issue would probably be that you're never actually saving their data. Your use of SetAsync is correct, it's just in the wrong location.
game.Players.PlayerRemoving:Connect(function(plr) local stats = plr:FindFirstChild("leaderstats") local stage = stats:FindFirstChild("Stage") PD:SetAsync(plr.UserId, Stage.Value) end)
be sure to remove any other calls to SetAsync