Why wont my leaderstats save, and what's wrong with this script?

Hi Developers,

I’m making saving leaderstats but it’s not saving once I leave, please tell me what’s wrong with this script?

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 Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats

local data 
local success, errormessage = pcall(function()
	data = playerData:GetAsync(player.UserId.."-Cash") 
end)

if success then 
	Cash.Value = data
else
	print("Error while getting your data")
	warn(errormessage)
end

end)

game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage = pcall(function()
	playerData:SetAsync(player.UserId.."-Cash", player.leaderstats.Cash.Value)
end)

if success then
	print("Data successfully saved!")
else
	print("There was an error while saving the data")
	warn(errormessage)
end

end)

1 Like

do you get any error messages? If not you should add some print statements to see how far it gets and what it is doing.

Not a problem with your code, this is fine. How exactly are you testing and finding out supposedly that data isn’t saving? If you’re testing in Studio then you need to make sure of three things:

  1. Your experience is published. This is so you can access certain settings and features.
  2. API services are enabled. Do this via Experience Settings.
  3. You’re setting values from the server in a Play Solo session. If you press F5/F6 and you’re viewing your own character, this emulates the client-side. Changes made this way do not propagate to the server. You need to hit the Test tab and change the environment to the server.

You should be fine then, but if not then it would be helpful if you employed basic debugging or provided any messages the console is giving you.

False. You have to manually create one, it doesn’t come one by default. Please don’t spread misinformation if you don’t know something.

To solve your issue, try testing this in a real experience.

1 Like

So, I give myself leaderstats, then I leave the game. (May I remind this isn’t studio this is real game.) Then I leave the game, rejoin and I have 0 leaderstats. You can see for yourself if you want.

Okay, I’ll buy that. I wasn’t sure and didn’t have my game code handy to look at.

I did, and it still didn’t work.

Could you try opening your developer console in the game to examine the output from the server?

Not descriptive enough. How are you giving yourself stats? Please provide detailed context as to what you’re doing. Screenshots of your hierarchy and such would also be helpful to understand how you’ve set up your environment. I would do the testing from Studio so you can actually observe the behaviours more clearly and investigate what happens when you leave the experience; you can start a local session and close the client window but keep the server one up to see what it does.

Or, trying adding a game:BindToClose() function, because sometimes PlayerRemoving event doesn’t fire when the last player of a server leaves.

Maybe it’s because where I am getting my leaderstats. I have this robbable ATM, and If I click on the meshes of money it gives me Leaderstats, but when I leave sometimes it says I have 500,000$ for other players and 0 for me.

I have to go anyway, see you guys tomorrow.

You tried to concatenate a number with a string.

Do this instead:

local success, errormessage = pcall(function()
	data = playerData:GetAsync(tostring(player.UserId).."-Cash") 
end)

My robbable leaderstat system.

Ok, so the leaderstats now save, but for other players it says I have 500k, anybody know how to fix?

I an having that same issue and its for me I think api service I can’t access game settings in roblox idk why.

Sounds like loading leaderstats problem.

Yes, you should listen to @ItzMeZeus_IGotHacked

yeah but it says " BindToClose is not a valid member of RBXScriptSignal" error.

show me your modfication to the script