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

How do I get my currency that saves to work?

Here is the script I used to script my currency that saves.


--Datastore service
local DSS = game:GetService("DataStoreService")
local Money = DSS:GetDataStore("Cash")


game.Players.PlayerAdded:Connect(function(plr)
 local pCash = Money:GetAsync(plr.UserId)or 10
 local leaderstats = Instance.new("Folder",plr)
 leaderstats.Name = "leaderstats"
 local CashValue = Instance.new("NumberValue")
 CashValue.Name = "Cash"
 CashValue.Value = pCash
 CashValue.Parent = leaderstats
 CashValue.Changed:connect(function(v)
  Money:SetAsync(plr.UserId,v)
  print("MoneySaved!")
 end) 
  end)

1 comment

0

dekkeda it is incorrect

1 answer

✓ Accepted answer

your code is very unclear i have made a better version for you.

local DataStoreService = game:GetService("DataStoreService")
local Money = DataStoreService:GetDataStore("Cash")

function playeradded(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = plr
	leaderstats.Name = "leaderstats"
	
	local Cash = Instance.new("NumberValue")
	Cash.Name = "Cash"
	Cash.Value = Money:GetAsync(plr.UserId) or 10
	Cash.Parent = leaderstats
end

function plrremoving(plr)
	Money:SetAsync(plr.UserId, plr.leaderstats.Cash.Value)
end

game.Players.PlayerAdded:Connect(playeradded)
game.Players.PlayerRemoving:Connect(plrremoving)

If this still don't work check if you have Api acces via studio enabled.

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