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

Why does this code error on the 7th time through the for loop?

lastnames = {"Aaron","Abel","Abernathy","Abney","Abraham","Acker","Ackerman","Adam","Adams","Adkins","Agee","Ahmed",
"Aiken","Ainsworth","Akers","Albert","Albertson","Alderman","Aldrich","Aldridge","Aleman","Alexander","Alford",
"Alger","Ali","Allen","Allison","Allman","Almond","Alonzo","Alston","Ambrose","Ammons","Amos","Anderson","Andrews",
"Angel","Anthony","Antoine","Antonio","Archer","Armstead","Armstrong","Arnett","Arrington","Arthur","Ashcraft",
"Ashford","Atchison","Atkins","Atkinson","Atwood","August","Augustine","Austin","Avery","Ayers","Bacon","Badger",
"Baer","Bagley","Bailey","Baker","Baldwin","Bales","Ball","Banks","Barber","Barbosa","Barden","Barfield","Barker",
"Barkley","Barlow","Barnard","Barnes","Barnett","Baron","Barrett","Barrow","Barry","Barton","Bass","Bachelor",
"Bates","Batten","Battle","Bauer","Baughman","Baumann","Baxley","Bayer","Baylor","Beach","Beal","Bean","Beasley",
"Beaver","Beckham","Beckman","Bedford","Beeler","Begley","Belanger","Bell","Belt","Bender","Benedict","Benjamin",
"Benner"}

firstnames = {"Andrew","Chris","David","James","Michael","Tim"}

positions = {"GK","CBL","CBR","FBL","FBR","AML","AMC","AMR","LST","CST","RST"}

game.Players.PlayerAdded:connect(function(user)
	team = Instance.new("ObjectValue",user)
	team.Name = "Team"
	starters = Instance.new("ObjectValue",team)
	starters.Name = "Starting 11"
	subs = Instance.new("ObjectValue",team)
	subs.Name = "Substitutes"
	reserves = Instance.new("ObjectValue",team)
	reserves.Name = "Reserves"
	for i = 1, 11 do
		local player = Instance.new("ObjectValue",starters)
		player.Name = lastnames[math.random(1,#lastnames)]
		local fn = Instance.new("ObjectValue",player)
		fn.Name = firstnames[math.random(1,#firstnames)]
		pos = Instance.new("StringValue",player)
		pos.Name = "Position"
		pos.Value = positions[i]
		table.remove(positions, i)
	end
end)

This errors on the 7th time through the for loop, but why does it work until it reaches that point?

1 answer

✓ Accepted answer

Using table.remove, all later values slide forward. Since you are deleting position the ith position (line 32), and i is incrementing, you don't actually delete everything in it in a normal order.

The state of the table looks like this (if instead it were filled with the numbers 1 to 11 to start):

Underscores indicate element to be deleted

{ _1_, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 } -- Before deleting the `1`
{ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }

{ 2, _3_, 4, 5, 6, 7, 8, 9, 10, 11 }
{ 2, 4, 5, 6, 7, 8, 9, 10, 11 }

{ 2, 4, _5_, 6, 7, 8, 9, 10, 11 }
{ 2, 4, 6, 7, 8, 9, 10, 11 }

{ 2, 4, 6, _7_, 8, 9, 10, 11 }
{ 2, 4, 6, 8, 9, 10, 11 }

{ 2, 4, 6, 8, _9_, 10, 11 }
{ 2, 4, 6, 8, 10, 11 }

{ 2, 4, 6, 8, 10, _11_ }
{ 2, 4, 6, 8, 10 }

Note that at the end, the selected element, i is 7; but the table is only 6 long.


The solution is to just take remove the first element from your table repeatedly:

pos.Value = table.remove(positions, 1);

(instead of lines 31 and 32)

Or, just remove nothing from the table positions.

1 comment

0

Thanks. I totally forgot about that.

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