Leader stat not saving

so i found a script, took it :wink: and modified it, and uh it didn’t work
edit: here’s the error:

502: API Services rejected request with error. HTTP 403 (Forbidden) 

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("playerData")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local gold = Instance.new("IntValue")
	gold.Name = "Wins"
	gold.Parent = leaderstats
	local po = Instance.new("IntValue")
	po.Name = "Points"
	po.Parent = leaderstats
	
	local data 
	local data2
	local success, errormessage = pcall(function()
		data = playerData:GetAsync(player.UserId.."Wins") 
		data2 = playerData:GetAsync(player.UserId.."Points") 
	end)
	
	if success then 
		gold.Value = data
		po.Value = data2
	else
		print("uh oh bad no save :(")
		warn(errormessage) --error line
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		playerData:SetAsync(player.UserId.."-Wins", player.leaderstats.Wins.Value)
		playerData:SetAsync(player.UserId.."-Points", player.leaderstats.Points.Value)
	end)
	
	if success then
		print("YAJ SAVED")
	else
		print("uh oh bad no save :(") -- da error 
		warn(errormessage)
	end
	
end)
1 Like

Some people have had this problem before (HTTP 403 (Forbidden) - #9 by DarkContinuum), fixed by setting place to public. Could also be that you don’t have API Services Allowed.

hmm i set it to public but still the error

Are API Services on for your game?

oh wait it is not on uhh one sec

hmm seems like theres no error but it dosn’t save either

In your saving part, you are saving using the key player.UserId…"-Wins", but in the loading, you did not put the -. You can either add the dash in the loading part or remove it in the saving.

1 Like

does the - do anything? or is it just decor?

The first parameter is a string value, if it has a -, it is a completely different value.
It would be saving with the key “00000-Wins” instead of “00000Wins”.

It is the key…
You really should learn more about DataStores before using them…

1 Like

so it will be like this?

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("playerData")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local gold = Instance.new("IntValue")
	gold.Name = "Wins"
	gold.Parent = leaderstats
	local po = Instance.new("IntValue")
	po.Name = "Points"
	po.Parent = leaderstats
	
	local data 
	local data2
	local success, errormessage = pcall(function()
		data = playerData:GetAsync(player.UserId.."-Wins") 
		data2 = playerData:GetAsync(player.UserId.."-Points") 
	end)
	
	if success then 
		gold.Value = data
		po.Value = data2
	else
		print("uh oh bad no save :(")
		warn(errormessage) --error line
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		playerData:SetAsync(player.UserId.."-Wins", player.leaderstats.Wins.Value)
		playerData:SetAsync(player.UserId.."-Points", player.leaderstats.Points.Value)
	end)
	
	if success then
		print("YAJ SAVED")
	else
		print("uh oh bad no save :(")
		warn(errormessage)
	end
	
end)

It should work, test it to find out.

nope, it didn’t print yaj saved

Add a return here and try again

If you are testing in studio, instead of stopping it, try running the command:

game.Players.LocalPlayer:Kick()

Not too sure why, but this works for me.

i leaved in-game and rejoined, but it didn’t seem to work

hmm nope, ima try add a print statement…oh wait i already have one

if it is saved it should print yaj saved, which it did…so does that mean i have to kick the player when they want to leave?

the kicking part is just for when your in studio, its probably some bug or something, im not 100% sure why.
you dont need to kick the player when they leave.

can you help me test it out in roblox then ;-; i have no way of knowing it saves or not in-game heres the game