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

Save/Load Value in datastores not working?

Asked by
DanzLua 2879 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

Hey, so today i've been trying to save a number value that is located in

workspace.Remotes.PlayerValues[playername].ExtraLives

Then loading the value into

workspace.Remotes.PlayerValues[playername].ChangeEL

But when I enter the game, change my value to something above 0, leave, then rejoin, the value doesn't load.

The following code is what I am using to Save/Load but it doesn't seem to be working and it doesn't give any errors.

ds = game:GetService("DataStoreService"):GetDataStore("Lives")

game.Players.PlayerAdded:connect(function(player)
    repeat wait() until workspace.Remotes.PlayerValues:FindFirstChild(player.Name)~=nil and workspace.Remotes.PlayerValues:FindFirstChild(player.Name).ChangeEL.Script.Disabled==false 
local lives = workspace.Remotes.PlayerValues:FindFirstChild(player.Name).ChangeEL
    lives.Value = ds:GetAsync(player.Name) or 0
end)

game.Players.PlayerRemoving:connect(function(player)
    ds:UpdateAsync(player.Name, function(oldValue) return workspace.Remotes.PlayerValues:FindFirstChild(player.Name).ExtraLives.Value end)
end)

game.OnClose = function()
    for i,v in pairs(game.Players:GetChildren()) do
        ds:UpdateAsync(v.Name, function(oldValue) return workspace.Remotes.PlayerValues:FindFirstChild(v.Name).ExtraLives.Value end)
    end
    wait(1)
end

Thank you.

0
If you're not using the old value, you can just use :SetAsync() instead of :UpdateAsync() einsteinK 145 — 7y
0
"workspace.Remotes.PlayerValues:FindFirstChild(player.Name).ExtraLives" will error if the playerthing isn't found, as FindFirstChild() would return nil, so you're doing (nil).ExtraLives, aka indexing a nil value. einsteinK 145 — 7y
0
No idea why it doesn't work, though. einsteinK 145 — 7y
0
@einsteinK Should I try changing :UpdateAsync() to :SetAsync() to see if that works? DanzLua 2879 — 7y
0
And "workspace.Remotes.PlayerValues:FindFirstChild(player.Name).ExtraLives" there is a ~=nil at the end of that but it doesnt seem to be showing. DanzLua 2879 — 7y

1 answer

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

Woah, super weird fix. ChangeEL was a numberValue so I tried changing it to a IntValue and it worked.

Ad

Answer this question