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

How to save currency after buying currency in game ?

So in my game I’ve made a currency shop and it’s working and everything but when you leave it doesn’t save and I’ve gone on YouTube and tried a bunch of data saving scripts but none are working this is my currency script please help me

game.Players.PlayerAdded:Connect(function(plr) local folder = Instance.new(“Folder”, plr) folder.Name = “leaderstats” local value = Instance.new(“IntValue”, folder) value.Name = “Money”

end)

Please help me :)

2 answers

Assuming you incorrectly used DataStores, To insert/modify data in a DataStore array you must use SetAsync, UpdateAsync and RemoveAsync.

SetAsync creates a new value and modifies it to the parameters specified.

Ex.

local DataStoreService = game:GetService("DataStoreService")
local ExampleStore = DataStoreService:GetDataStore("ExampleData")

ExampleStore:SetAsync("ExamplePlayer", 50)

This creates a new value ExamplePlayer and sets it's corresponding value to 50.

UpdateAsync modifies an existing value to the parameters specified.

Ex.

local DataStoreService = game:GetService("DataStoreService")
local ExampleStore = DataStoreService:GetDataStore("ExampleData")

ExampleStore:UpdateAsync("ExamplePlayer", 50)

This changes ExamplePlayer's corresponding value to 50

RemoveAsync removes a value in the array.

Ex.

local DataStoreService = game:GetService("DataStoreService")
local ExampleStore = DataStoreService:GetDataStore("ExampleData")

ExampleStore:RemoveAsync("ExamplePlayer")

This removes ExamplePlayer and its corresponding value

For more see Data Stores.

Here you go hope this helps also put this in server script service! Any questions? Just ask me!

local datastore = game:GetService("DataStoreService") -- DONT CHANGE
local ds1 = datastore:GetDataStore("GemSaveSystem") -- DONT CHANGE
local ds2 = datastore:GetDataStore("CashSaveSystem") -- DONT CHANGE

game.Players.PlayerAdded:connect(function(plr)
 local folder = Instance.new("Folder", plr)
 folder.Name = "leaderstats"
 local gems = Instance.new("IntValue", folder)
 gems.Name = "HouseTickets" -- Change to your Currency
 local cash = Instance.new("IntValue", folder)
 cash.Name = "Cash" -- Change to your Currency
 
 gems.Value = ds1:GetAsync(plr.UserId) or 0
 ds1:SetAsync(plr.UserId, gems.Value)
 
 cash.Value = ds2:GetAsync(plr.UserId) or 0
 ds2:SetAsync(plr.UserId, cash.Value)
 
 gems.Changed:connect(function()
  ds1:SetAsync(plr.UserId, gems.Value)
 end)
 
 cash.Changed:connect(function()
  ds2:SetAsync(plr.UserId, cash.Value)
 end)
end)

3 comments

0

OMG IT WORKED THANK YOU SO MUCH Bssiemeow

0

OMG IT WORKED THANK YOU SO MUCH Bssiemeow

0

Thats all good :)

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