Save new value after reset

local SaveNewStage = game:GetService("DataStoreService"):GetDataStore("saveStage")
local function saveData(plrId: string| number, data: any)
	local success, err = pcall(function()
		return SaveNewStage:SetAsync(plrId, data)
	end)
	if not success then
		return err
	else
		return "Saved new data for " .. plrId
	end
end
game:GetService("ReplicatedStorage").resetStage.OnServerEvent:Connect(function(plr)
	local stageVal = plr:WaitForChild("leaderstats").Stage
	stageVal.Value = 1
	local checkpoint = workspace.Checkpoints["1"]
	if checkpoint then
		local char = plr.Character or plr.CharacterAdded:Wait()
		char:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(checkpoint.Position + Vector3.new(0, 1, 0))
		plr:GetAttribute("StageValue")
		saveData(plr.UserId, plr:WaitForChild("leaderstats").Stage.Value)
		if stageVal then
			local saved = saveData(plr.UserId, 1)
			plr:WaitForChild("leaderstats").Stage.Value = 1
			return "saved data for " .. plr.UserId
		end
	else
		warn("Checkpoint not found.")
	end 
end)```

im making a obby where the player has the option to reset the stage if they want to back to stage 1, that works but when i rejoin i'm back at where i reset my stage, like if i reset i go to 1 then when i rejoin im back at stage 38. if any other script is needed i'll provide it
3 Likes

Looking at your code, You seem to be saving your current Stage and then overwriting it with saveData(plr.UserIId, 1). Maybe removing that and just updating the leaderstats is what you need :smiley:

1 Like
	if stageVal then
		plr:WaitForChild("leaderstats").Stage.Value = 1
		return "saved data for " .. plr.UserId
	end

like this?

1 Like

that looks good to me, does it not work?

1 Like

You can check the code if you want. I think i fixed it. Compare them one by one and revert if you think i deleted something you really need.

local SaveNewStage = game:GetService("DataStoreService"):GetDataStore("saveStage")
local function saveData(plrId: string| number, data: any)
	local success, err = pcall(function()
		SaveNewStage:SetAsync(plrId, data)
	end)
	if not success then
		return err
	else
		return "Saved new data for " .. plrId
	end
end
-- Make sure resetStage is called on the client. (Very Important)
game:GetService("ReplicatedStorage").resetStage.OnServerEvent:Connect(function(plr)
    -- You could remove the WaitForChild functions since it is an RemoteEvent and the player has to be loaded to execute this.
	local stageVal = plr:WaitForChild("leaderstats").Stage
	-- 
	local checkpoint = workspace.Checkpoints["1"]
	if checkpoint then
		local Char = plr.Character or plr.CharacterAdded:Wait()
		Char:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(checkpoint.Position + Vector3.new(0, 1, 0))
		if stageVal then
			saveData(plr.UserId, stageVal.Value)
			stageVal.Value = 1
			return "saved data for " .. plr.UserId
		end
	else
		warn("Checkpoint not found.")
	end 
end)
1 Like

i already got it but thank you for trying to help :slight_smile:

1 Like

We resolved this on discord. The issue was that when player left the saving used an attribute called “StageValue” on the player but this was never set to 1. Here is the updated code

game:GetService("ReplicatedStorage").resetStage.OnServerEvent:Connect(function(plr)
    local stageVal = plr:WaitForChild("leaderstats").Stage
    stageVal.Value = 1
    local checkpoint = workspace.Checkpoints["1"]
    if checkpoint then
        local char = plr.Character or plr.CharacterAdded:Wait()
        char:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(checkpoint.Position + Vector3.new(0, 1, 0))
        plr:SetAttribute("StageValue", 1) -- This part we forgot
        if stageVal then
            plr:WaitForChild("leaderstats").Stage.Value = 1
            return "saved data for " .. plr.UserId
        end
    else
        warn("Checkpoint not found.")
    end 
end)
1 Like

Yeah, i observed that the line plr:GetAttribute("StageValue") was unused. Good that it got fixed

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.