Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago
Edited 3 years ago
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

Log in to vote
0
Answered by 3 years ago

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.

0
I did what you said, but still only 3 of the 6 values inside the array get removed/inserted. Why? User#34929 0 — 3y
0
Every single value inside of the array is either a number, string, or userdata, yes? DeceptiveCaster 3761 — 3y
0
every value is a Team User#34929 0 — 3y
0
Have you checked the output for any errors? DeceptiveCaster 3761 — 3y
View all comments (7 more)
0
yes there are none User#34929 0 — 3y
0
This is likely one of those scenarios where you're the one having issues, I tried this myself and had no problems. DeceptiveCaster 3761 — 3y
0
Do you mind printing the contents of ChosenTeams? DeceptiveCaster 3761 — 3y
0
I did it printed out: Mad Scientists, Grand Wolves, Black Ravens, Hunters, Jesters, Arsonists before removing and Grand Wolves, Hunters, Arsonists after removing User#34929 0 — 3y
0
So it appears as if it's grabbing every 2nd index. Do EXACTLY table.insert(Teams, Team). DeceptiveCaster 3761 — 3y
0
I already did User#34929 0 — 3y
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. DeceptiveCaster 3761 — 3y
Ad
Log in to vote
-1
Answered by 3 years ago
Edited 3 years ago

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.

0
The issue is not the specification of what gets assigned to the table, it's the assignation of values to the table. DeceptiveCaster 3761 — 3y

Answer this question