I cant save data from the leaderstats the way that i want to

i am currently facing a problem with the script that i have to save the data from the leaderstats of my players.

--//Services
local DataStoreService = game:GetService("DataStoreService") --We need DataStoreService to save our data. 
local Players = game:getService("Players")

--//DataStore
local CurrencyData = DataStoreService:GetDataStore("CurrencyData") --This is the name under which the database will be saved. Here we have to give a name to our Database. The name must be unique, because if there is another Database in your game with the same name, it can lead to errors.

--//Configuration
local CurrencyName = "Robux" --This is your currency name. This name will appear on the leaderboard. (Ex: Cash, Money, Coins, Clicks, etc.)
local StartingValue = 1000 --We will set this number to our value when we create it. You can give some Starting Cash to your players.
Players.PlayerAdded:Connect(function(player) --Detects when a new player joins. We also create a variable with the player.
	local UserData
	local success, errMsg = pcall(function() --We use pcall because sometimes the DataStore go down and we do not want the script to have errors.
		UserData = CurrencyData:GetAsync(player.UserId) --We try to get user Data using ":GetAsync()" 
	end)

	if success == false then --In case "success" returns false, it means that DataStores are down again. :/
		local doNotSave = Instance.new("Folder")
		doNotSave.Name = "DoNotSave"
		doNotSave.Parent = player
		--We create a folder called DoNotSave. When the player disconnects we will check if this folder exists, and if it does we will NOT save the data.
	else
		print("Data loaded!") --We let the player know that their data has been loaded successfully.
	end

	local leaderstats = Instance.new("Folder")  --We are creating the leaderstats folder. Here we will insert our Values.
	leaderstats.Name = "leaderstats" --If you want to see the currency in the default leaderboard keep the name ,,leaderstats''.
	leaderstats.Parent = player

	local Currency = Instance.new("IntValue") --Now we are creating an IntValue using Instance.new
	Currency.Name = CurrencyName --We set the name of the Value with the name we set above, in the variable "CurrencyName".
	Currency.Value = UserData or StartingValue --Now we set the value to what we received from the DataStore. If there is nothing (the player did not play) we set the value to StartingValue.
	Currency.Parent = leaderstats --Storing the IntValue inside of leaderstats.
end)
Players.PlayerRemoving:Connect(function(player) --When a player lefts or disconnects from the game we are saving the data
	local SavingPath = player.leaderstats:FindFirstChild(CurrencyName) --We are trying to find the IntValue

	if player:FindFirstChild("DoNotSave") then --We are trying to find the folder that is created if the data was not received successfully.
		warn("Player data was not saved to avoid data loss.")
	else
		CurrencyData:SetAsync(player.UserId, SavingPath.Value) --If the data was received successfully, then we can save the data without any problems.
	end
end)

this code somewhat works, its weird to explain;
if i go shift + f9 and give myself points, then it saves the data, however it doesn’t save my data when i add or multiply scores using scripts. Can someone help? (default is 1000 value)

1 Like

Maybe you add points to yourself through a local script? Although I don’t think it will be that simple.

1 Like

Can we see the out put and script that controls betting?

Check the way you are adding points, you need to add via server script or else
you will see the updated points but server won’t. So it’ll save the old points

2 Likes

this seems to be the problem, i’ll try to fix it when i get home later today.

1 Like

Also try your script in a local server since player removing doesn’t always fire when playtesting.

It does fire because it saved for me a few days ago even when I didn’t start a local server.

That’s what I mean, I’m suggesting checking for inconsistencies within the data saving by using a local server since the player removing event might not always fire while playtesting.

1 Like

okay i found a solution:

i created a RemoteEvent in ReplicatedStorage to handle communication between the client (local script) and the server (server script).

i set up the server script to handle player data loading and saving using the DataStore.
The server listens for the RemoteEvent to receive updates from the client about the currency changes.
When the event is received, the server script validates the data, subtracts the spending amount, calculates the reward based on the multiplier, and updates the player’s currency accordingly.

it’s all working now :wink:

2 Likes

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