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

Why isn't this loop scanning the entire table?

Hello!

For a game I'm making, I have this map changing script. Generating the map goes perfectly fine, but there are certain objects on the map that have special properties, thus needing to be cloned from the Server Storage.

I have one of each of these objects (Throwables) in the Storage. The following snippet of code is supposed to look at a table in another module, scan through with a for loop, and copy each item that is called for, using data stored with the object to place it properly. It's supposed to make multiple instances of each object, placing each one according to the data stored with each entry.

for index,model in pairs(mapdata[choice]) do

	print(index)

	if game:GetService("ServerStorage")["Throwables"][index] ~= nil then

		local new_model = game:GetService("ServerStorage")["Throwables"][index]:clone()
		new_model.Parent = workspace
		new_model.Hitbox.CFrame = model.pos
		om:MakeThrowable(new_model)	--Don't need to worry about this.
		for i,v in pairs(new_model:GetChildren()) do
			model.Anchored = false
		end

	end

end

The issue, however, is that it only clones one of each object. It doesn't scan through the entire table, it just grabs one of each object! I don't know why.

If it helps, I'll post the table it's looking at below, with a large section cut out because it IS quite a big table.

maps = {
	["Farm"] = {
		["Barrel"] = {name = "Barrel"; pos = CFrame.new( -156.920013, 12.4000006, -38.6800003, 1, 0, 0, 0, -1, 0, 0, 0, -1 )};
		["Barrel"] = {name = "Barrel"; pos = CFrame.new( -96.7200012, 31.8000011, 48.1200027, 0, 0, 1, 0, 1, -0, -1, 0, 0 )};
		["Barrel"] = {name = "Barrel"; pos = CFrame.new( -45.920002, -2.70000005, 118.920006, 0.258819044, 0.965925813, 1.71743892e-007, 2.95362756e-008, 1.69888153e-007, -1, -0.965925813, 0.258819044, 1.54404631e-008 )};
		["Barrel"] = {name = "Barrel"; pos = CFrame.new( -51.5200043, 1.30000007, 124.520004, 0, 0, 1, 0, 1, -0, -1, 0, 0 )};
		["Barrel"] = {name = "Barrel"; pos = CFrame.new( -53.7200012, -2.70000005, 120.520004, 0, 0, 1, 0, 1, -0, -1, 0, 0 )};
		["Barrel"] = {name = "Barrel"; pos = CFrame.new( -49.7200012, -2.70000005, 125.120003, 0, 0, 1, 0, 1, -0, -1, 0, 0 )};
		["Barrel"] = {name = "Barrel"; pos = CFrame.new( -54.1200027, -2.70000005, 124.920006, 0, 0, 1, 0, 1, -0, -1, 0, 0 )};
		["Apple"] = {name = "Apple"; pos = CFrame.new( -59.470005, -0.200000584, 25.3700027, 0, 0, 0.99999994, 0, 0.999999881, 0, -0.99999994, 0, 0 )};

........................................

		["Chair"] = {name = "Chair"; pos = CFrame.new( -39, -1.50000012, 72.8000031, 0, 0, 1, 0, 1, -0, -1, 0, 0 )};
		["Table"] = {name = "Table"; pos = CFrame.new( -39.25, -2.70000005, 79.0500031, 0, 0, 1, -1, 0, 0, 0, -1, 0 )};
		["Table"] = {name = "Table"; pos = CFrame.new( -70.6500015, -2.80000019, 28.4500008, 0, 1, -0, -1, 0, 0, 0, 0, 1 )};
		["Chair"] = {name = "Chair"; pos = CFrame.new( -73.4000015, -1.60000002, 33, 0, 0, -1, 0, 1, 0, 1, 0, 0 )};
		["Chair"] = {name = "Chair"; pos = CFrame.new( -67.2000046, -1.60000002, 33, 0, 0, -1, 0, 1, 0, 1, 0, 0 )};
		["Chair"] = {name = "Chair"; pos = CFrame.new( -77.4000015, -1.60000002, 28.6000023, -1, 0, 0, 0, 1, 0, 0, 0, -1 )};
		["Chair"] = {name = "Chair"; pos = CFrame.new( -63.6000023, -1.60000002, 28.6000023, 1, 0, 0, 0, 1, 0, 0, 0, 1 )};
		["Chair"] = {name = "Chair"; pos = CFrame.new( -78.7990036, 44.2000008, 58.7980042, 0, 0, -1, 0, 1, 0, 1, 0, 0 )};
		["Barrel"] = {name = "Barrel"; pos = CFrame.new( -160.920013, 12.4000006, -41.6800003, 1, 0, 0, 0, -1, 0, 0, 0, -1 )};
		["Barrel"] = {name = "Barrel"; pos = CFrame.new( -158.920013, 16.4000015, -39.8800011, 1, 0, 0, 0, -1, 0, 0, 0, -1 )};
		["Chicken"] = {name = "Chicken"; pos = CFrame.new( 99.3470078, -2.73700023, 15.5040007, 3.39172061e-007, -2.98022833e-008, -0.999997854, -2.69968883e-008, 0.999997497, 1.49011408e-008, 0.999998748, -5.39937801e-008, -3.2071668e-007 )};
		["Chicken"] = {name = "Chicken"; pos = CFrame.new( -91.1980057, -2.93700004, -52.3500023, 0.999998748, -5.39937801e-008, -3.2071668e-007, -2.69968883e-008, 0.999997497, 1.49011408e-008, -3.39172061e-007, 2.98022833e-008, 0.999997854 )};
		["Boulder"] = {name = "Boulder"; pos = CFrame.new( -43.6199951, 72.4000015, 292.600037, 0, 0, -0.99999994, 0, 0.999999881, 0, 0.99999994, 0, 0 )};
		["Log"] = {name = "Log"; pos = CFrame.new( -155.020004, 19.9000015, -26.7800007, 0.258819044, 0, 0.965925813, 0, 1, 0, -0.965925813, 0, 0.258819044 )};
	};
}

2 comments

0

You really just need to separate the items with commas, not semicolons, so the table recognizes a new item.

1

False; semicolons and commas are interchangably usable in Lua tables as separators.

1 answer

✓ Accepted answer

What is happening is that every same-name index in that table is replacing the previous entries of the same name.

tab = {}

tab.a = "Name"
tab.a = "Not Name"

print(tab.a) --> Not Name

You're going to have to give the indexes in the table unique names.

Since you have a 'Name' property within the sub-tables themselves, you could just nix the named indices entirely, and use ipairs on the table.

2 comments

1

I honestly feel kinda embarrassed now. The answer is so simple I can't believe I didn't see it. :p That being said, what do you mean about 'nixing the named indices entirely, and using ipairs'? Don't entries in a table have to be named?

1

Nope. If you don't set an index, they're automatically given an integer index. (Basically, nixing the index is syntactic sugar for specifying it as #tab + 1)

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