im trying to save multiple currency
when i tested it, it didn’t save any currency
i tried to find the error but i couldn’t
please help
local datastoreservice = game:GetService(“DataStoreService”)
local playerdata = datastoreservice:GetOrderedDataStore(“playerdata”)
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "cash"
cash.Parent = folder
local xp = Instance.new("IntValue")
xp.Name = "XP"
xp.Parent = folder
local playerId = 'player_'..player.UserId
local data = playerdata:GetAsync(playerId)
if data then
cash.Value = data['cash']
xp.Value = data['XP']
print("data loaded")
else
cash.Value = 0
xp.Value = 0
print("New player")
end
end)
local function createtable(player)
local playerstats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
playerstats[stat.Name] = stat.Value
end
return playerstats
end
local function onplayerexit(player)
local playerstats = createtable(player)
local success, err pcall(function()
local playerId = ‘player_’…player.UserId
playerdata:SetAsync(playerId, playerstats)
end)
end
game.Players.PlayerRemoving:Connect(onplayerexit)