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

getasync() not working to load play time data?

Asked by 3 years ago
Edited 3 years ago

Basically what my code does is save an int value that's the seconds you've played in the game. The problem is that when you join and onplayeradded is ran, that timeplayed value wont load and it will start back from 0

dss=game:GetService('DataStoreService')

inventorystore=dss:GetDataStore('InventoryStore')
statsstore=dss:GetDataStore('StatsStore')

function storeconverter(store)
    if store=='InventoryStore'then
        store=inventorystore
    elseif store=='StatsStore'then
        store=statsstore
    end
    return store
end

function load(plr,store,storetype)
    newstore=storeconverter(store)
    local success,errormessage=pcall(function()
        return newstore:GetAsync(plr.UserId..storetype)
    end)
    if not success then

        warn(errormessage)
    else
        warn('success save')
    end
end

function save(plr,store,storetype,data)
    newstore=storeconverter(store)
    local success,errormessage=pcall(function()
        newstore:SetAsync(plr.UserId..storetype,data)
    end)
    if not success then

        warn(errormessage)
    else
        warn('success save')
    end
end

game.Players.PlayerAdded:Connect(function(plr)--init
    local plrid=plr.UserId

    thething=load(plr,'StatsStore','-timeplayed')
    local leaderstats=game.ReplicatedFirst.leaderstats:Clone()
    leaderstats.timeplayed.Value=thething
    leaderstats.Parent=plr

end)
game.Players.PlayerRemoving:Connect(function(plr)--savedata
        --save all data
        save(plr,'StatsStore','-timeplayed',plr.leaderstats.timeplayed.Value)


        print('complete save')
end)

The key plr.userid-timeplayed is saved in a store called statsstore. anyone know why the loading wont work?

1 answer

Log in to vote
0
Answered by 3 years ago

SOLVED apparently return doesnt work in pcalls i think?

working code for the load function

function load(plr,store,storetype)
    newstore=storeconverter(store)
    local success,errormessage=pcall(function()
        newstore=newstore:GetAsync(plr.UserId..storetype)
    end)
    if not success then
        --failsafe:SetAsync(plr.UserId,true)
        --plr:Kick('Error occured while loading data')
        warn(errormessage)
    else
        --failsafe:SetAsync(plr.UserId,false)
        warn('success load')
        return newstore
    end
end
Ad

Answer this question