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

Can anyone figure out why my save/load script won't work?

Asked by
Mr1Vgy 30
9 years ago

Can anyone figure out why my save/load script won't work? I've tested it many times and can't figure out why it doesn't save...

game.Players.PlayerAdded:connect(function(plr)
    local key = "user_"..plr.userId
    plr1 = plr
    data_points:UpdateAsync(key, function(oldValue)
        local newValue = oldValue or 0
        repeat
            wait()
            plr:FindFirstChild("Points")
        until plr:FindFirstChild("Points")== true
        plr.Points.Value = newValue
        print"POINTS LOADED!"
        --return newValue
    end)

    data_wins:UpdateAsync(key, function(oldValue1)
        local newValue1 = oldValue1 or 0
        repeat
            wait()
            plr:FindFirstChild("Wins")
        until plr:FindFirstChild("Wins")== true
        plr.Wins.Value = newValue1
        print"WINS LOADED!"
        --return newValue1
    end)

    while true do
        wait(5)
        data_points:SetAsync("Points", plr.Points.Value)
        data_wins:SetAsync("Wins", plr.Wins.Value)
        print'saved!'
    end
end)

1 answer

Log in to vote
0
Answered by
Merely 2122 Moderation Voter Community Moderator
9 years ago

Thisloop - it looks like you're trying to wait for a child named "Points" to be added. However, it will never escape the loop since it keeps trying to compare the object to true, which will always be false.

repeat
    wait()
    plr:FindFirstChild("Points")
until plr:FindFirstChild("Points")== true

UpdateAsync is supposed to be used when you're trying to update the value. In your case, you just need to get the value, so you should be using GetAsync.

game.Players.PlayerAdded:connect(function(plr)
    local key = "user_"..plr.userId
    plr1 = plr
    local points = data_points:GetAsync(key) or 0
    plr:WaitForChild("Points")
    plr.Points.Value = points
    print("Points loaded")

This code is really inefficient, and it may cause you to run into the DataStore request limit. Instead of setting it every 5 seconds, you should probably only update it when the player leaves (use the PlayerRemoving event)

    while true do
        wait(5)
        data_points:SetAsync("Points", plr.Points.Value)
        data_wins:SetAsync("Wins", plr.Wins.Value)
        print'saved!'
    end
Ad

Answer this question