i cant save the data when the player leave the game, but every time i try to save the player coins the function dont work :(
local DataStoreService = game:GetService("DataStoreService") local dataCentral = { coins = 0, } local datas = {} local canRun = false function loadData(player) datas[player] = {} datas[player].coins = 0 for i, v in pairs(dataCentral) do if not DataStoreService:GetDataStore(i):GetAsync(tostring(player.UserId)) then DataStoreService:GetDataStore(i):SetAsync(tostring(player.UserId), v) else dataCentral[i] = DataStoreService:GetDataStore(i):GetAsync(tostring(player.UserId)) datas[player][i] = dataCentral[i] warn(DataStoreService:GetDataStore('coins'):GetAsync(tostring(player.UserId))) end end local stat = Instance.new("IntValue") stat.Name = "leaderstats" local cash = Instance.new("IntValue") cash.Name = "Coins" cash.Value = dataCentral.coins cash.Parent = stat stat.Parent = player if not canRun then canRun = true check() end end function check() while true do for i, v in pairs(game.Players:GetChildren()) do if v.leaderstats.Coins.Value ~= DataStoreService:GetDataStore('coins'):GetAsync(tostring(v.UserId)) then if v.leaderstats.Coins.Value ~= datas[v].coins then datas[v].coins = v.leaderstats.Coins.Value end end end wait() end end function saveData(player) DataStoreService:GetDataStore('coins'):SetAsync(tostring(player.UserId), datas[player].coins) end game:GetService("Players").PlayerAdded:connect(loadData) game:GetService("Players").PlayerRemoving:connect(saveData)
The error is in the function saveData()
"DataStoreService:GetDataStore('coins'):SetAsync(tostring(player.UserId), datas[player].coins)". You are setting datas[PLAYER].coins. Im pretty sure this is the issue. Try implimenting a auto save loop to get error outputs. Try datas[player.UserId]. Dont forget to change line 10 aswell.