Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why coins (dataStorage) aren't saving when player leave the game?

Asked by 6 years ago
Edited 6 years ago

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

0
:Connect not :connect connect is deprecated User#19524 175 — 6y
0
that won't cause it to not work though justoboy13 153 — 6y
0
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) justoboy13 153 — 6y
0
nothing works :( Chrystian_PPP 0 — 6y

1 answer

Log in to vote
0
Answered by
H4X0MSYT 536 Moderation Voter
6 years ago

"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.

Ad

Answer this question