Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i fix my checkpoint script?the data always go back to 1.

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)

1 answer

✓ Accepted answer

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

2 comments

0

Ok,now the values save,but my checkspawn function still not spawning the player on the checkpoints,for some reason my checkspawn function isn't running because the obby isn't printing.

0

Nevermind I just did the code wrong game.Players.ChildAdded:Connect(checkspawn), here it's actually workspace and GetPlayerFromCharacter(players)  here it's actually just player From Character.Now the script is working. 

This discussion is preserved as it was. New answers, comments, and votes are closed.