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

[Unsolved] Why is not everything inside the array removed/inserted?

local EvilCount = 0
local GoodCount = 0
local NeutralCount = 0

repeat
    local index = math.random(1,#Evil)
    currentteam = Evil[index]
    print(currentteam.Name.." was chosen for this round.")
    currentteam.Parent = game.Teams
    table.insert(ChosenTeams, currentteam)
    table.remove(Evil, index)
    wait(1)
    EvilCount = EvilCount+1
until EvilCount == 2

repeat
    local index = math.random(1,#Good)
    currentteam = Good[index]
    print(currentteam.Name.." was chosen for this round.")
    currentteam.Parent = game.Teams
    table.insert(ChosenTeams, currentteam)
    table.remove(Good, index)
    wait(1)
    GoodCount = GoodCount+1
until GoodCount == 2

repeat
    local index = math.random(1,#Neutral)
    currentteam = Neutral[index]
    print(currentteam.Name.." was chosen for this round.")
    currentteam.Parent = game.Teams
    table.insert(ChosenTeams,currentteam)
    table.remove(Neutral, index)
    wait(1)
    NeutralCount = NeutralCount +1
until NeutralCount == 2

print(table.unpack(ChosenTeams))

for i, Team in pairs(ChosenTeams)
do
	table.remove(ChosenTeams, i)
	table.insert(Teams, Team)
	wait(0.1)
end

print(table.unpack(ChosenTeams))

2 answers

The issue lies on this line:

table.insert(Teams, 1, Team)

This line tells the script to:

  • Refer to the Teams table for insertion.

  • Assign index 1 to Team. If a value already exists at index 1, it will be overwritten.

The second bullet is where things are going wrong. Every time the for loop performs an iteration, it writes the Team value to the 1st index of the Teams table. Because it keeps doing this, the result is always going to be the 1st index of the Teams table being the last index of the ChosenTeams table, and the Teams table always having a length of 1.

If you tell table.insert() to just insert the value without giving it a number, like so:

table.insert(Teams, Team)

The value Team will go to the next empty index (so, for example, if index 1 contains a value, move to index 2 and insert a value into that index). This prevents any value in the table from being overwritten.

11 comments

0

I did what you said, but still only 3 of the 6 values inside the array get removed/inserted. Why?

0

Every single value inside of the array is either a number, string, or userdata, yes?

0

every value is a Team

0

Have you checked the output for any errors?

0

yes there are none

0

This is likely one of those scenarios where you're the one having issues, I tried this myself and had no problems.

0

Do you mind printing the contents of ChosenTeams?

0

I did it printed out: Mad Scientists, Grand Wolves, Black Ravens, Hunters, Jesters, Arsonists before removing and Grand Wolves, Hunters, Arsonists after removing

0

So it appears as if it's grabbing every 2nd index. Do EXACTLY table.insert(Teams, Team).

0

I already did

0

Ok, so after some tweaking, I noticed something quite odd. table.remove() shifts the position of the last value removed. What I now suggest is for you to do table.insert(Teams, Team) ONLY in the loop, then run another loop to remove every value from the initial table using table.remove(). See if that works.

I have a team splitter that might help.. First off I think your over complicating stuff already.

local plrs = game.Players:GetPlayers()

local Green = -- Change these and put your teams
local Blue = 
local Red = 

	for i,v in pairs(plrs) do
 		if i > #plrs/2 then 
			v.Team = Red
  		else
			v.Team = Blue
  		end
	end
end

I have no idea if this fits your question.. but if it does then it might help.

1 comment

0

The issue is not the specification of what gets assigned to the table, it's the assignation of values to the table.

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