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

Why doesn't this table recognize the values set to it?

So basically I make a new table and insert values into it:

function save(player)
	local base = player.Team.Base.Value
	playerdata = {}
	for i,v in pairs(base.Placed:GetChildren()) do
		local pX = table.insert(playerdata, "pX" .. i) 
		local pY = table.insert(playerdata, "pY" .. i)
		local pZ = table.insert(playerdata, "pZ" .. i)
		pX = v.Position.X - base.Base.Position.X
		pY = v.Position.Y - base.Base.Position.Y
		pZ = v.Position.Z - base.Base.Position.Z
		local sX = table.insert(playerdata, "sX" .. i)
		local sY = table.insert(playerdata, "sY" .. i)
		local sZ = table.insert(playerdata, "sZ" .. i)
		sX = v.Size.X
		sY = v.Size.Y
		sZ = v.Size.Z
		local rot = table.insert(playerdata, "rot")
		rot = math.abs(v.Orientation.Y/90)
		local shape = table.insert(playerdata, "shape")
		shape = v.Shape.Value
	end
	local encode = HttpService:JSONEncode(playerdata)
	print(encode)
	ds:SetAsync(player.UserId, encode)
end

save(game.Players.Player1)

But, when I print this off here's what I get: ["pX1","pY1","pZ1","sX1","sY1","sZ1","rot","shape","pX2","pY2","pZ2","sX2","sY2","sZ2","rot","shape"] no values for the variables were saved, also the table is surrounded by brackets (Honestly no idea why this is the case, could also be the problem). This should be an expected outcome but obviously isn't working as intended: '{"pY2":4,"sX2":5,"sY2":2,"sZ2":1,"shape1":1,"pZ1":2,"shape2":0,"rot1":1,"pX1":20,"pY1":4,"pX2":20,"pZ2":2,"rot2":1,"sZ1":1,"sY1":2,"sX1":5}' Any ideas why this is the case? anything could help, Thanks.

2 answers

✓ Accepted answer

You assign to local variables pY the value you want to store, but then don't put it in playerdata. You are using playerdata like a list, but based on the expected output, you want to use it like a dictionary.

-- Example of assigning to a dictionary
playerdata["pY2"] = 4
-- using your variables:
playerdata["pY" .. i] = v.Position.Y - base.Base.Position.Y

-- Later, to load the information back:
local pY = playerdata["pY" .. i]

Notably, you'll need to either save the number of items in the table (so you can do for i = 1, numItems do), or keep increasing 'i' and trying to load the next item until the keys no longer exist (meaning you've loaded all the items already).

You may find it easier to save a list of items, like this:

playerdata = {}
playerdata.items = {} -- only needed if you want to save other things to playerdata
 for i,v in pairs(base.Placed:GetChildren()) do
	playerdata.items[i] = {
		pX = v.Position.X - base.Base.Position.X,
		--etc
	}
end

In datastores, each table must be either a list (ie has integer keys) or a dictionary, not both. Specifically, you should either always use integer keys, or never use them (for any one table). In the above, there playerdata is a dictionary, items is a list, but each item is another dictionary, so it should work.

Hi! I think you just need to change the i's to v's in the table.insert statements. The v's would indicate the value in your for statement.

See example below:

local pX = table.insert(playerdata, "pX" .. v)

Let me know if that works!

Cheers, WindNT :D

2 comments

0

sadly that's not the issue, that part of the code only has to do with the naming of the variable within the table. Thanks anyways

0

No Problem! Thanks for letting me know :D

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