Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I save data currency if I already have a game currency?

I am still new to coding. I have two things I want to save, money and rebirth. Money in my game works like a clicker game and every click earns money.

All I can do is make the money but when I leave game it does not save. I have not put in a data saver yet because I don't know how

All the tutorials I watched and read create a new currency but I want to alter it so it uses the currency I already have. I do not know what script to write for that.

Please help.

1 answer

You want to use DataStoreService to save your values. Now the way you organize your datastores is up to you and everyone has different ways of doing it. You could create two datastores, one for money and one for rebirth. Or, you could use one datastore which stores a dictionary of both values. This is what I'll be doing in my example.

local DataStoreService = game:GetService("DataStoreService") 
local playerData = DataStoreService:GetDataStore("MoneyRebirthDS") --You can give the datastore any name you want
local emptyData = { --These are the default values players will get when they join for the first time
	["Money"] = 0,
	["Rebirth"] = 0
}
game.Players.PlayerAdded:Connect(function(player)
	--I assume you already have this function somewhere, where you set up your money you say you already have. You can set up the saved data in the same function.
	local success, data = pcall(function() --Wrapping your datastore requests in a pcall adds extra safety as datastore requests sometimes fail for various reasons
		return playerData:GetAsync(player.UserId) 
	end)
	if success then
		data = data or emptyData --If no saved data exists the defaults will be used
		local playerMoney = data["Money"]
		local playerRebirth = data["Rebirth"]
	end	
end)

--This next function fires when the player leaves, this is where we will save the data
game.Players.PlayerRemoving:Connect(function(player)
	local newData = {
		["Money"] = player.Money --Put the path to where you store players' money values
		["Rebirth"] = player.Rebirth --Same as above but for Rebirth
	}

	local success, errorMessage = pcall(function()
		playerData:SetAsync(player.UserId, newData) --You can also use UpdateAsync if you prefer
	end)
	if not success then --This will let you know if there were any errors in saving
		print(errorMessage)
	end
end)

I haven't tested the script so I don't know if it works perfectly or not, but those are the basic elements you need in a data saving system. Some people prefer to also save every few minutes or so, incase the game crashes and the data fails to save when the player leaves. Read up on DataStoreService, Roblox gives us a lot of documentation on it for us to learn everything about it. Let me know if you have any other questions!

1 comment

0

Thank you! So I did everything you said but it still did not for some reason. I wondered if maybe it was because the script I wrote for keeping track of the leaderstats was making a new folder each time you enter a server so maybe that is the problem to why it keeps going back to zero?? I would show you my leaderstats script but I don't know how

This discussion is preserved as it was. New answers, comments, and votes are closed.