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

Why does this UpdateAsync not work and doesn't even print out any error?

Asked by 4 years ago

I've be trying to get get this chunk of code to save the players coins when they leave; I don't mean to use SetAsync either. The first two print (the CoinValue and the string inside the pcall) prints, but the prints at the bottom doesn't print (the if statements nor the print under the end). Needless to say, the CoinValue isn't being updated. And I've also checked that newValue isn't nil from the print(CoinValue) and it is a number.

Players.PlayerRemoving:Connect(function(player)
    local key = tostring(player.UserId)
    local CoinValue = PlayerCoins:FindFirstChild(key).Value
    local CoinStore = DataStoreService:GetDataStore("CoinStore")
    print(CoinValue)

    local success, Result = pcall(function()
        print("running function")
        CoinStore:UpdateAsync(key,function(oldVal)
            local newVal = CoinValue
            if newVal ~= nil then
                return newVal
            else
                return oldVal
            end
        end)
    end)

    print(success)

    if success then
        print("yay")
        print(success)
        print(Result)
    else 
        print("nope it failed")
    end
end)

1 answer

Log in to vote
0
Answered by 4 years ago

Figured it out. Due to this being ran in Studio, it timed out before the full script was ran. Solved by adding a BindToClose function with a wait(3) in it.

Ad

Answer this question