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

Trying to save multiple values from a table doesn't save but why?

Last time I didn't really get good answers so I'm hoping someone can help me this time.

I tried doing a "for loop" because everyone kept telling me to use that but I don't know how to though!

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

game.Players.PlayerAdded:connect(function(player)
	
	local key = "user-" .. player.userId
	
	
	local store = DataStore:GetAsync(key)
	
	store[1] = {}
	
	local items = store[1]
	
	
	for i,v in pairs(player.Class.WeaponStats:GetChildren()) do
		if v:IsA("Configuration") then
			
			for _, c in pairs(v:GetChildren()) do
				
				if c:IsA("Configuration") then
					
					for _, l in pairs(c:GetChildren()) do
						
						if l:IsA("NumberValue") then
							
							table.insert(l.Value)
							
						end
						
					end
					
				end
				
			end
			
		end
	end
	
	print("Life")
	
	
		
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local items = {}
	
		for i,v in pairs(player.Class.WeaponStats:GetChildren()) do
		if v:IsA("Configuration") then
			
			for _, c in pairs(v:GetChildren()) do
				
				if c:IsA("Configuration") then
					
					for _, l in pairs(c:GetChildren()) do
						
						if l:IsA("NumberValue") then
							
							table.insert(l.Value)
							
						end
						
					end
					
				end
				
			end
			
		end
	end

	local key = "user-" .. player.userId
	
	DataStore:SetAsync(key, items)
	
	print("Ended")
end)

1 answer

✓ Accepted answer

Alright so a couple of problems. In player removed you want to change line 60 to table.insert(items, l.value), table.insert takes two to three arguments, the first is always the table, the second is the value you're inserting, the optional argument determines if you put it somewhere specific.

Additionally in player added

	    local store = DataStore:GetAsync(key)
	 
	    store[1] = {}
	 
	    local items = store[1]

store is a table by default, you are then setting the first entry to an empty table, overwriting anything that was in that spot at the time. It isn't saving that, but you won't be able to load the first value you saved anymore. You want to iterate on store, not store[1], then find the weapon that corresponds to that value.

It seems like you're not too confident using tables, it might help you to go read documentation on tables before you continue.

3 comments

0

I still don't get it...

0

You're saving values to a table in player removed, then in player added you overwrite one of the values. Remove line 10 and 12. Second change line 60 as I said in the first paragraph. Third you need to rewrite lines 15-37, duplicating what you put in player added isn't going to work, and it seems you don't understand the basics enough to write it on your own.  Read up on tables and loops and you m

0

will do

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