Adding Currency Problem

Hi everyone, so in one of my games I have a currency system, wich is linked to a datastore. The problem is that I want to add some currency every 5s (for example). And I dont know why, it doesnt work. And when adding currency I would like that it saves up in the datastore. If anybody could help me I would really appreciate.

Thx

can we see your script for currency?

yeah sure, let me send u a pic

(Curency) image Data Store for “Suricoins’s currency”

The first pic includes “Minutes” DataStore, wich works perfectly, the problem is in the second currency

I’m not trying to be rude, but the way you save currency overwrites itself will not and doesnt work.

1 Like

I perfectly understand, i mean, im not a programer, im a builder, so has u may imagine, my scripting skills are very bad. So, ill listen to you

alright, I’ll adjust changes to your script. Give me a minute or 2
here is the finished script:

local dataservice = game:GetService("DataStoreService")
local statsstore = dataservice:GetDataStore("StatsData")

game.Players.PlayerAdded:Connect(function(player)
	local statstable = statsstore:GetAsync(player.UserId) or {}
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Suricoins"
	cash.Value = statstable.Suricoins or 0
	cash.Parent = leaderstats
	
	local Time = Instance.new("IntValue")
	Time.Name = "Minutos"
	Time.Value = statstable.Minutos or 0
	Time.Parent = leaderstats
end)

game.Players.PlayerRemoving:Connect(function(player)
	local stats = {
		["Suricoins"] = player.leaderstats.Suricoins.Value,
		["Minutos"] = player.leaderstats.Minutos.Value
	}
	statsstore:SetAsync(player.UserId, stats)
end)

game:BindToClose(function()
	for i,v in pairs(game.Players:GetChildren()) do
		local stats = {
			["Suricoins"] = v.leaderstats.Suricoins.Value,
			["Minutos"] = v.leaderstats.Minutos.Value
		}
		statsstore:SetAsync(v.UserId, stats)
	end
end)
1 Like

sure, if u need anything else tell me

so, a quick question, if i modify this script, and i replace it with this one, will the previous data be lost??

yes, but unless this is a game that many people have played and have a lot of data, it shouldnt matter.

so when i replace the script ill need give the people back manually their currency value?

no, I can just adjust it so it will automatically get their data and slowly replace it (when they join the game).

okay, let me replace the script

I’ll adjust it real quick so you can have their data not be lost and just be a quick transfer over time.

1 Like

can you try an add a function that add 1 to the minutes value every 60s and 100 value to suricoins currency every 60s??

here is the new script:

local dataservice = game:GetService("DataStoreService")
local statsstore = dataservice:GetDataStore("StatsData")
local oldtime = dataservice:GetDataStore("DsTime")
local oldsuricoins = dataservice:GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(player)
	local statstable = statsstore:GetAsync(player.UserId) or {}
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Suricoins"
	cash.Value =  statstable.Suricoins or oldsuricoins:GetAsync("id_"..player.UserId) or 0
	cash.Parent = leaderstats
	
	local Time = Instance.new("IntValue")
	Time.Name = "Minutos"
	Time.Value = statstable.Minutos or oldtime:GetAsync(player.UserId.."-tim") or 0
	Time.Parent = leaderstats
	while wait(60) do
		Time.Value += 1
		cash.Value += 100
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local stats = {
		["Suricoins"] = player.leaderstats.Suricoins.Value,
		["Minutos"] = player.leaderstats.Minutos.Value
	}
	statsstore:SetAsync(player.UserId, stats)
end)

game:BindToClose(function()
	for i,v in pairs(game.Players:GetChildren()) do
		local stats = {
			["Suricoins"] = v.leaderstats.Suricoins.Value,
			["Minutos"] = v.leaderstats.Minutos.Value
		}
		statsstore:SetAsync(v.UserId, stats)
	end
end)

this should work for getting old data and eventually starting to replace it.
just finished adding the function. It should work now.

1 Like

alright, sure. I’ll try to implement that function lol

srry if im asking to much lol, but u are pretty good scripting, so i would like to solve most of my scripting problems, im so sorry if im annoying

image