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

DataStore service not saving?

Hello I'm learning how to make data stores and made one. However it is giving me issues that aren't displayed in the output so I don't know what is causing the issue.

I have a leader board with 2 stats one is an int value and the other is a string value. I have a part that gives me points to the int value and I want this data store to save the newly added points when I leave the game so that if I returned to the game the points will still be there. However when I tested it, the points are not saved and displayed as 0 (Original amount).

This is my script.


local DataStore = game:GetService("DataStoreService"):GetDataStore("Strength")


game.Players.PlayerAdded:Connect(function (player)
	
	local stats = Instance.new("IntValue", player)
	stats.Name = "leaderstats"
	
	local SpiritClass = Instance.new("StringValue", stats)
	SpiritClass.Name = "SpiritClass"
	SpiritClass.Value = "E"
	
	local Spirit = Instance.new("IntValue", stats)
	Spirit.Name = "Spirit"
	
	local key = "Player_"..player.UserId
	
	local savedvalues = DataStore:GetAsync(key)
	
	if savedvalues then
		-- if the player has played before then it'll load there stats
		-- format of saving is {SpiritClass, SpiritStrength}
		SpiritClass.Value = savedvalues[1]
		Spirit = savedvalues[2]
	else
		local valuesToSave = {SpiritClass.Value, Spirit.Value}
		DataStore:SetAsync(key, valuesToSave)
	end
	
end)

game.Players.PlayerRemoving:Connect(function (player)
	
	local key = "Player_"..player.UserId
	
	--save key: {Spiritclass, Spirit}
	local valuesToSave = {player.leaderstats.SpiritClass.Value, player.leaderstats.Spirit.Value}
	DataStore:SetAsync(key, valuesToSave)
	
end)

1 answer

✓ Accepted answer

All your script is right, but you have two errors. Both errors about saving data. If you want to save multiple data you need to save it like this

game.Player.PlayerRemoving:Connect(function(player)
	local key = "Player_"..player.UserId

	DataStore:SetAsync(key,{
		player.leaderstats.SpiritClass.Value; -- This is a first one
		player.leaderstats.Spirit.Value; --Second
	})
	--and if you saving multiple data you need to put ";" this before typing next
	--that means you have another info
end

-- and on line 24 put "Value" word
Spirit.Value = savedvalues[2]

5 comments

0

So I say valuesToSave = {player.leaderstats.SpiritClass.Value; player.leaderstats.Spirit.Value;} ?

0

Yes but only thing that you need to make info like a list

0

Thank you!

0

The way this is being saved is correct. Both ; and ,  are valid for a list.

0

The error was not using.Value on line 24

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