I'm sure the title says it all, I've been twiddling with this script for a while trying to figure out, trying all sorts of things, to make it save both values but it just won't.
local currencyName = "Rubies" -- Name of currency
local maincurrencyName = "Coins"
local DataStore = game:GetService("DataStoreService"):GetDataStore("RubiesDataStore") -- How to save amount each player has
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder") -- Where the data will be stored
folder.Name = "leaderstats"
folder.Parent = player
local currency = Instance.new("IntValue") -- The currency amount
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 = 10
print("New Player")
end
end)
local DataStoreT = game:GetService("DataStoreService"):GetDataStore("CoinsDataStore") -- How to save amount each player has
game.Players.PlayerAdded:Connect(function(player)
local folder = player:WaitForChild("leaderstats")
local maincurrency = Instance.new("IntValue")
maincurrency.Name = maincurrencyName
maincurrency.Parent = folder
local IDT = maincurrencyName.."-"..player.UserId
local savedDataT = nil
pcall(function()
savedDataT = DataStore:GetAsync(IDT)
end)
if savedDataT ~= nil then
maincurrency.Value = savedDataT
print("Data Loaded")
else
-- New player
maincurrency.Value = 100
print("New Player")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local ID = currencyName.."-"..player.UserId
DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
local IDT = maincurrencyName.."-"..player.UserId
DataStoreT:SetAsync(IDT,player.leaderstats[maincurrencyName].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)

1 comment
Ugh. Unnecessary code, could have shortened into like.. half the lines of code, also the bind to close doesnt look helpful tbh, just put everything in a single PlayerAdded