Why odes my Data Store not work

Hello, I am just starting out with scripting so bear with me. I am currently working on an Obby (for simplicity’s sake, not so much coding). I mostly watch Tutorials on how to do things and try to understand the Code that is presented.
My problem: I want to store the Players current Stage in a Data Store so when they close the game and rejoin later they can continue and don’t need to start over.

The following is my current code for the tracking of the Players Stage and the saving of the current Stage, however every time i complete Stage 1, the number updates in the Leaderboard, but when i rejoin i restart at Stage 0 (aka the beginning).

local CPoints = game.Workspace.Room1.Checkpoints
local CBoxes = game.Workspace.Room1.Checkboxes
local DSService = game:GetService("DataStoreService")
local StageStore = DSService:GetDataStore("StageStore")
local Players = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Parent= leaderstats
	
	local StageData
	local success, errormsg = pcall(function()
		StageData = StageStore:GetAsync(player.UserId.."-Stage")
	end)
	if stage.Value ~= nil then
		stage.Value = StageStore
	end
	if success then
		print("Data loaded.")  --Inclouded for troubleshooting so I can see wether the Data was loaded
		print(StageStore:GetAsync(player.UserId.."-Stage", player.leaderstats.Stage.Value)) --Included for troubleshooting and my attempt to print out the current value of the DataStore
	else 
		print(errormsg)
	end
	
	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		wait()
		char:MoveTo(CPoints[stage.Value].Position)
		
		hum.Touched:Connect(function(hit)
			if hit.Parent == CBoxes then
				if tonumber(hit.Name) == stage.value + 1 then
					stage.Value = stage.Value + 1
				end
			end
		end)	
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	
			
	local success, errormsg = pcall(function()
		StageStore:SetAsync(player.UserId.."-Stage", player.leaderstats.Stage.Value)
	end)
	
	if success then 
		print("Data saved.")	--Inclouded for troubleshooting so I can see wether the Data was saved
		print(StageStore:GetAsync(player.UserId.."-Stage", player.leaderstats.Stage.Value)) --Included for troubleshooting and my attempt to print out the current value of the DataStore
	else
		print(errormsg)
		end	
end)

Have you tried testing this in Roblox and not studios itself. Roblox studios has a habit of PlayerRemoving not firing in time when testing by your self in studios

Around 20 minutes ago I also had an issue where my DataStore was not working.
However, now DataStore is working for me.

If it doesn’t work for you right now it’s probably an issue into your code. Let me know and I’ll see if I can help you out a bit.

This looks to be correct? Maybe a roblox studio issue sometimes my data doesn’t save or it blocks my requests.

Just published it and tested it, still doesn’t work so I guess it has to be a issue with my code.

I even tried publishing and trying it that way. Has to be a issue with my code i guess.

Could you send the code you used just for the DataStore script?

Thank you, the code is already included in the post tho.

Yes, but I don’t know which part is used for datastore?
Are you using two scripts?

One script should be about creating a folder called leaderstats and putting your values in it
The other is what actually stores your data
Could you just send the part that your using to store your data

Are you also getting any errors in your output?

No I dont get any erorrs in the output.
I have only one script because I figured when I already have a function that is connected to .PlayerAdded
(the function takes care of creating the .leaderstats folder and the tracking of the Stages inside the leaderboard) I just put the Data loading part in the same function.
And i added another function that is connected to .PlayerRemoving after the .PlayerAdded functions ends that takes care of saving the Data.

Would just like to ask about this:

local DSService = game:GetService("DataStoreService")
local StageStore = DSService:GetDataStore("StageStore")

Why are you using two different variables for DSService and your DataStore?
You could just do

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

Probably not the solution btw if you were wondering

I see in your script you have a print statement when your data is loaded
Does that appear in the output?

Yeah the one from GetAsync gets printed:

00:23:35.378 Data loaded. - Server - CheckPointScriptTest:25
00:23:35.378 1 Instance - Server - CheckPointScriptTest:26

The one from SetAsync doen’t tho, so i guess it doesn’t fire

Not really sure if this is the solution but,
It may be because you are using CharacterAdded rather than PlayerAdded
Most of the time when you are loading a players GlobalDataStore, you use PlayerAdded

But don’t I use PlayerAdded tho? The CharacterAdded comes later in the Script, doesn’t it?

player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		wait()
		char:MoveTo(CPoints[stage.Value].Position)

		hum.Touched:Connect(function(hit)
			if hit.Parent == CBoxes then
				if tonumber(hit.Name) == stage.value + 1 then
					stage.Value = stage.Value + 1
				end
			end
		end)	
	end)
end)

Btw I just realized now the print from the Data saving prints too. It displays the right Value but when i spawn in the Stage Value in the Leaderboard gets set back to 0.

I see what you mean with the CharacterAdded but I thought it just covers the part of teleporting the Player to the current Checkpoint.

Bit confused on what you’re saying here?
Also try this bit of code instead

game.Players.PlayerAdded:Connect(function(plr)
		local hum = plr:WaitForChild("Humanoid")
		wait()
		plr:MoveTo(CPoints[stage.Value].Position)

		hum.Touched:Connect(function(hit)
			if hit.Parent == CBoxes then
				if tonumber(hit.Name) == stage.value + 1 then
					stage.Value = stage.Value + 1
				end
			end
		end)	
	end)
end)