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()

4 comments
:Connect not :connect connect is deprecated
that won't cause it to not work though
can you use an object as a position in an array? maybe try datas[player.Name] and see if that works(I am new to arrays)
nothing works :(