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

Datastore not working correctly?

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

game.Players.PlayerAdded:connect(function(player)

	local Folder = Instance.new("Folder",player)
	Folder.Name = "Folder"
	
	local BubblePoints = Instance.new("IntValue",Folder)
	BubblePoints.Name = "Points"
	
	local BubbleLevel = Instance.new("IntValue",Folder)
	BubbleLevel.Name = "Level"
	
	local BubbleCoins = Instance.new("IntValue",Folder)
	BubbleCoins.Name = "Coins"
	
	local key = "player-"..player.UserId
	
	local SavedValues = DataStore:GetAsync(key)
	
	if SavedValues then
		BubblePoints.Value = SavedValues[0]
		BubbleCoins.Value = SavedValues[1]
		BubbleLevel.Value = SavedValues[2]
	else
		local valuesToSave = {0, 1, 2}
		DataStore:SetAsync(key,valuesToSave)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local key = "player-"..player.UserId
	
	local ValuesToSave = {0, 1, 2}
	
	DataStore:SetAsync(key, ValuesToSave)
end)

I'm trying to save three different values, but none of them are actually saving. Where did I go wrong? XD

2 answers

✓ Accepted answer

Lua tables are indexed from 1 not 0 so this:-

BubblePoints.Value = SavedValues[0] -- there is not index 0
BubbleCoins.Value = SavedValues[1]
BubbleLevel.Value = SavedValues[2]

Needs to be changed to:-

BubblePoints.Value = SavedValues[1] 
BubbleCoins.Value = SavedValues[2]
BubbleLevel.Value = SavedValues[3]

You seems to be saving the table correctly and loading the data correctly, you can unpack an array to see all of the data e.g.

print(unpack([array herer]))

this will print all of the content of the array.

Hope this helps.

1 comment

0

Ohh okay. Mybad, Thank you!

Including what kingcom5 stated about arrays in Lua, the problem is when the player leaves you're resetting the datastore to your 'ValuesToSave' variable, which is a set table and doesn't actually consist of any values from the Player.

Get the values from the player!

game.Players.PlayerRemoving:connect(function(player)
	local key = "player-"..player.UserId
	local ValuesToSave = {
		player.Points.Value
		player.Coins.Value, 
		player.Level.Value
	}
	DataStore:SetAsync(key, ValuesToSave)
end)

1 comment

0

Got it >:D

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