In my game there is a coins and level value and the coins you get in the game are on a base number multiplied by your level value. I have a datastore for level and coin values, but only the coins value is saving. Here is the main server side script in sever script service that manages currencies and datastore:
local currencyName = "Coins"
local levelo = "Level"
local DataStore = game:GetService("DataStoreService"):GetDataStore("Coins") --Do not change "Coins"
local DataStoreL = game:GetService("DataStoreService"):GetDataStore("Level") --Do not change "Level"
-- DO NOT MESS WITH THE DATASTORE NAMES OR ALL PLAYER DATA WILL BE LOST --
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 level = Instance.new("IntValue")
level.Name = levelo
level.Parent = folder
local ID = currencyName.."-"..player.UserId
local savedData = nil
local IDL = levelo.."-"..player.UserId
local savedDataL = nil
pcall(function()
savedData = DataStore:GetAsync(ID)
end)
pcall(function()
savedDataL = DataStore:GetAsync(IDL)
end)
if savedData ~= nil then
currency.Value = savedData
else
currency.Value = 50
end
if savedDataL ~= nil then
level.Value = savedDataL
else
level.Value = 1
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local ID = currencyName.."-"..player.UserId
DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)
game.Players.PlayerRemoving:Connect(function(player)
local IDL = levelo.."-"..player.UserId
DataStoreL:SetAsync(IDL,player.leaderstats[levelo].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("The game is shutting down right now, your data will be saved")
end
end
wait(5)
end)
btw i put "dont mess with the values" because I'm working on it with a friend

4 comments
Use a table. and use only one SetAsync.
Line 67 doesn't even save the data, what a scam
@TTChaos LOL
Don't worry that kick message won't appear either