Checkpoint DataStores Are Not Saving

local Players = game:GetService("Players")

local DataStore = game:GetService("DataStoreService")
local StageStore = DataStore:GetDataStore("Stage")

local Stages = workspace:WaitForChild("CheckpointStages")

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

	local Stage = Instance.new("IntValue")
	Stage.Parent = leaderstats
	Stage.Name = "Stage"
	
	local StageData

	local success, errorMessage = pcall(function()
		StageData = StageStore:GetAsync(player.UserId)
	end)

	if success then
		if StageData then
			local character = player.Character or player.CharacterAdded:Wait()
			if player.leaderstats.Stage.Value == 0 then
				local character = player.Character or player.CharacterAdded:Wait()
				character:PivotTo(Stages:FindFirstChild(tostring(1)).CFrame + Vector3.new(0,5,0))
			else
				character:PivotTo(Stages:FindFirstChild(tostring(player.leaderstats.Stage.Value)).CFrame + Vector3.new(0,5,0))
			end
		else
			local character = player.Character or player.CharacterAdded:Wait()
			character:PivotTo(Stages:FindFirstChild(tostring(1)).CFrame + Vector3.new(0,5,0))
		end
		print("Success!")
	else
		warn(errorMessage)
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local StageData
	
	local success, errorMessage = pcall(function()
		StageData = StageStore:SetAsync(player.UserId, player.leaderstats.Stage.Value)
	end)

	if success then
		if StageData then
			print("Saved Data for "..player.Name)
		else
			print("Failed to Save Data for "..player.Name)
		end
		print("Success!")
	else
		warn(errorMessage)
	end
end)

game:BindToClose(function()
	task.wait()
end)

I also have a script outside of this that changes leaderstats.Stage’s value so that it can actually change every time you go to the next stage.

The problem I’m having is it not saving. All the prints say "Success!" but nothing gets saved. I don’t really ever work with DataStores so I’m a noob for these types of problems.

Any help is appreciated!

1 Like

Hi @xft2008.

I tried your script and it works perfectly. However, you forgot to mention that when you retrieve the data from StageStore:GetAsync(player.UserId), you’re not setting Stage.Value to the retrieved data.

You can easily put a
Stage.Value = StageData or 0

Let me know if you fixed it, and if you have question ask them!

2 Likes

Wow that was really simple, thanks! It works perfectly now.

1 Like

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