Hello i don’t normally script,
so i’m trying to make a saving currency script but when i update my currency and leave and join back its the basic again.
i have no errors in the console please help!
thanks
Can you send the script you’re using so I can see what’s happening?
sure here
local currencyName = “£”
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 = nil
pcall(function()
savedData = DataStore:GetAsync(ID)
end)
if savedData ~= nil then
currency.Value = savedData
print("Data loaded")
else
-- New player
currency.Value = 1
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()
-- When game is ready to shutdown
for i, player in pairs(game.Players:GetPlayers()) do
if player then
player:Kick("This game is shutting down")
end
end
wait(5)
end)
yes Enable studio Access to API services are on.
That work for me:
local datastore = game:GetService(“DataStoreService”)
local ds1 = datastore:GetDataStore(“CashSaveSystem”)
game.Players.PlayerAdded:connect(function(plr)
local folder = Instance.new("Folder", plr)
folder.Name = "leaderstats"
local cash = Instance.new("IntValue", folder)
cash.Name = "Cash"
cash.Value = ds1:GetAsync(plr.UserId) or 250
ds1:SetAsync(plr.UserId, cash.Value)
cash.Changed:connect(function()
ds1:SetAsync(plr.UserId, cash.Value)
end)
end)
local ds = game:GetService("DataStoreService")
local playerdata = ds:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
local dataCache
local done , err = pcall(function()
dataCache = playerdata:GetAsync(player.UserId.."-money")
end)
if dataCache ~= nil and done then
money.Value = dataCache
else
print(player.Name .. "'s datapool has returned nil, set data table.")
warn(err)
playerdata:SetAsync(player.UserId .. "-money", player.leaderstats.Money.Value)
end
end)
local SaveData = function(player)
local success, errormessage = pcall(function()
playerdata:UpdateAsync(player.UserId.."-money", function(OldValue)
return player.leaderstats.Money.Value
end)
end)
if success then
print("Player's Data has been saved")
else
print("There was an error saving player's data")
warn(errormessage)
end
end
game.Players.PlayerRemoving:Connect(SaveData)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
SaveData(player)
end
end)
Ill try them both out now
thanks
It’s better practice if you use protected call in case Roblox’s servers go down, your entire code won’t just break.
any idea were i put the script? is it server script storage?
Yes, it will only function through the server.
i think im doing something wrong when i update the money in players>justhatsav>leaderstats>money i updated the value to 500 left and rejoined then it was back on 0
any ideas what im doing wrong?