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

Why isn't my value saving when I did everything right?

Okay I know I'm not tripping... This code has nothing wrong with it, yet it still doesn't save my value. PLEASE explain. I beg

Even the output agrees with me that it saved until I rejoin.

local ValueDataStore = game:GetService("DataStoreService"):GetDataStore("SaveValueDataStore")

--Save Value's Value
game.Players.PlayerAdded:Connect(function(player)
local SaveThisValue= player:WaitForChild("PlayerValues",1):FindFirstChild("SaveThisValue")
	local valuekey = "Player_"..player.UserId
	local GetValuesVal = ValueDataStore:GetAsync(valuekey) or 0
	
	SaveThisValue.Value = GetValuesVal
	ValueDataStore:SetAsync(valuekey, SaveThisValue.Value, wait(1))
	print("Saved Value!")
end)

game.Players.PlayerRemoving:Connect(function(player)
	local SaveThisValue= player:WaitForChild("PlayerValues",1):FindFirstChild("SaveThisValue")
    ValueDataStore:SetAsync(player.UserId, SaveThisValue.Value)
end)

What happens is, the output says that this value is saved as true, but when I join the game(the actual game), the value is set back to false... Roblox explain

Everything im testing in is the real game, i dont test in studio

2 comments

0

Maybe remove 'Wait(1)' on line 10? also try using 'game:BindToClose(function() wait(3) end)' so the server stays up until 3 seconds for everything to save.

0

What do you mean by join the game? It won't save the value from the game's studio, only the actual in-game value (unless you have studio api enabled, in which the reverse is true)

2 answers

It’s because when you leave, you’re not saving it to the correct key. The key when you load it first is Player_(userid), while the key you save to is just the userid.

To fix this, just replace player.UserId with “Player_”..player.UserId

1 comment

0

Still didnt work but good try tho

Ok so firstly, as usual, this is for a Script in ServerScriptService. I added the cloning function to the script so you have less memory taken up by the script in Workspace. Here's the code:

local ValueDataStore = game:GetService("DataStoreService"):GetDataStore("SaveValueDataStore")
 
--Save Value's Value
game.Players.PlayerAdded:Connect(function(player)
	local folder = game.Workspace.PlayerValues:Clone()
	folder.Parent = player
	
	local SaveThisValue = player:WaitForChild("PlayerValues", 1):FindFirstChild("SaveThisValue")
	local valuekey = ("Player_"..player.UserId)
	
	SaveThisValue.Value = ValueDataStore:GetAsync(valuekey) or false
	
	ValueDataStore:SetAsync(valuekey, SaveThisValue.Value)
	wait(1)
	print("Saved Value!")
end)
 
game.Players.PlayerRemoving:Connect(function(player)
	local valuekey = ("Player_"..player.UserId)
	local SaveThisValue = player:WaitForChild("PlayerValues", 1):FindFirstChild("SaveThisValue")
	ValueDataStore:SetAsync(valuekey, SaveThisValue.Value)
end)

The two problems with this were the wait within the function for the setasync (removing it and choosing a comma did not give the option for a variable) so i moved that down a line. The other problem is that your valuekeys were not the same between the Save and the Load functions.

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