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

Database UpdateAsync and SetAsync not working?

Asked by
vkax 85
6 years ago

I don't know what I've done wrong, I've played it in the real game and in the studio one and it won't work, I enabled api stuff in studio but it still doesn't work.

local ds = game:GetService("DataStoreService"):GetDataStore("Points")

game.Players.PlayerAdded:connect(function(player)
    -- datastore stuff
    game.ServerStorage.leaderstats:Clone().Parent = player
    local PointsValue = ds:GetAsync(player.UserId)
    if PointsValue then
        player.leaderstats.Points.Value = PointsValue else
        player.leaderstats.Points.Value = 0
    end

    while wait(5) do
        player.leaderstats.Points.Value = player.leaderstats.Points.Value + 300
    end
end)


game.Players.PlayerRemoving:connect(function(player)
    ds:UpdateAsync(player.UserId,player.leaderstats.Points.Value)
end)
0
Please post your error. User#19524 175 — 6y
0
As far I can see, you are using updateasync instead of setasync. abnotaddable 920 — 6y
0
setasync will have cache sometimes and get async will get the wrong value, fixed it btw vkax 85 — 6y

1 answer

Log in to vote
0
Answered by
yoavstr 52
6 years ago
Edited 6 years ago

Hey, The problem was that in line 19 you are using UpdateAsync(), you should use SetAsync()

I fixed for you:

local ds = game:GetService("DataStoreService"):GetDataStore("Points")

    game.Players.PlayerAdded:connect(function(player)
        -- datastore stuff
        game.ServerStorage.leaderstats:Clone().Parent = player
        local PointsValue = ds:GetAsync(player.UserId)
        if PointsValue then
            player.leaderstats.Points.Value = PointsValue 
            else
            player.leaderstats.Points.Value = 0
        end

        while wait(5) do
            player.leaderstats.Points.Value = player.leaderstats.Points.Value + 300
        end
    end)


    game.Players.PlayerRemoving:connect(function(player)
        ds:SetAsync(player.UserId,player.leaderstats.Points.Value)
    end)

Make sure you have enabled Studio Access to API Services

  1. Go to the following link: https://www.roblox.com/develop
  2. Click on the gear icon, choose "Configure Game"
  3. Check - Enable Studio Access to API Services

If you got any further problems please comment on this answer and I will do my best to help.

0
Actually, sometimes SetAsync with cache but it doesn't matter anyways. vkax 85 — 6y
Ad

Answer this question