Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Save/Load Value in datastores not working?

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.

5 comments

0

If you're not using the old value, you can just use :SetAsync() instead of :UpdateAsync()

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.

0

No idea why it doesn't work, though.

0

@einsteinK Should I try changing :UpdateAsync() to :SetAsync() to see if that works?

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.

1 answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.