Money wont save

did you ever change it when testing the new script? Because it’s working fine for me.

any idea how to do that? yeah i dont have a clue eek

this is the script
local currencyName = “coins”
local DataStore = game:GetService(“DataStoreService”):GetDataStore(“TestDataStore”)
game.Players.PlayerAdded:Connect(function(player)

local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player	

local currency = Instance.new("IntValue")
currency.Name = currencyName
currency.Parent = folder

local ID = currencyName.."-"..player.UserId
local savedData	= DataStore:GetAsync(ID)

if savedData then
	currency.Value = savedData
	print("Data loaded")
else
	-- New player
	currency.Value = 5
	print("New player to the game")
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local ID = currencyName…“-”…player.UserId
DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
if player then
local ID = currencyName…“-”…player.UserId
DataStore:SetAsync(ID, player.leaderstats[currencyName].Value)
end
end
end)

local DataStore = DataStoreService:GetDataStore("TestDataStore") -- change TestDataStore

change “TestDataStore” to something else.

ok im on it now i changed it to DataStore

keep in mind that if you get 5 gain its because you’re setting 5 to be the default value when the player has no data and if you join again the data will still be 5 because you haven’t changed it to anything higher than 5 so it stills save as 5

1 Like

oh so the easiest solution is to remove the new player getting 5?

becasue it turned back to 5 when i changed it in studio

not sure what you mean but I recommend you to increase the 5 to something else when you click a part via ClickDetector or a TextButton via RemoteEvent and then check if the data did saved, the click detector option should be easier because you don’t have to rely on remote events and just use a script (the player object will be passed as parameter).

that is happening because you need to be in server perspective to be able to make changes on a Instance, if you do it directly through the client perspective the changes will only affect the client which means that the server will not recognize the changes.

this is what im doing to change the leader stat image image image

but are you currently in server perspective?

yes i am in the server perspective

I will write you a data store code based off yours

thank you Liam. :smiley: :+1: .

I did not tested it, but I believe it should work fine.

local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")

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

	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player	

	local currency = Instance.new("IntValue")
	currency.Name = "Coins"

	local ok, result = pcall(DataStore.GetAsync, DataStore, player.UserId)

	if ok then -- you must check if you successfully retrieved the data
		
		if result then -- if the player has data
			currency.Value = result
		else -- if the player doesn't have data
			currency.Value = 5
		end
		
		currency.Value += 5 -- every time you play the game the currency will increase by 5, this will let you know if the data is saving
		
		currency.Parent = folder -- you should always make changes and then parent the Instance
	else
		warn(result) -- if we failed to retrieve data, result will be a warning message
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local Coins = player.leaderstats.Coins
	local ok, result = pcall(DataStore.SetAsync, DataStore, player.UserId, Coins.Value)
	
	if ok then -- if we successfully saved the data
		warn("data has been saved")
	else
		warn(result) -- once again, if we failed, result will be a warning message
	end
end)

game:BindToClose(function()
	for _,player in ipairs(game.Players:GetPlayers()) do
		coroutine.wrap(function()
			local Coins = player.leaderstats.Coins
			pcall(DataStore.SetAsync, DataStore, player.UserId, Coins.Value) -- we don't need to check there because its pointless (not in studio).
		end)
	end
end)

so that should work for me? ill test it out now

ok i got 10 coins now let me leave and join back

ok i still got 10 coins let me add more on to see if that works

if its not working, there should be warning message in your output pointing what’s wrong and what you shall fix