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

Why does this script choose the same Teams over and over again?

Asked by 4 years ago

The following script chooses the same Teams over and over again even tho they are parented to Teams:

nan = 0

repeat
    currentteam = Teamfolder[math.random(1,#Teamfolder)]
    print(currentteam.Name.." was chosen for this round.")
     currentteam.Parent = game.Teams
    wait(1)
    nan = nan+1
until nan == 5

Why?

2 answers

Log in to vote
1
Answered by
DevingDev 346 Moderation Voter
4 years ago
Edited 4 years ago

You need to remove the element from the table after you've selected it. Take this analogy as an example; you've entered a lottery, but when you win your name doesn't get removed, the paper note with your name written on it gets thrown back into the hat again. That means you still have a chance of winning again.

table.remove(table, index) will do the job for you. It'll remove the element you want gone and it will push the other elements forwards so there won't be a gap. It'll still work with ipairs.

Teamfolder[index] = nil will also remove the element, but it'll leave a gap in the table, and will now not work with ipairs.

But since you're not using ipairs here, either should do the trick for you.

local Teamfolder = {workspace.Team1, workspace.Team2, workspace.Team3, workspace.Team4}

local nan = 0

repeat
    local index = math.random(1,#Teamfolder)
    currentteam = Teamfolder[index]
    print(currentteam.Name.." was chosen for this round.")
    currentteam.Parent = game.Teams
    table.remove(Teamfolder, index)
    wait(1)
    nan = nan+1
until nan == 5
0
Oh. So that's what she/he tried to do! Great answer! haba_nero 386 — 4y
0
, User#34929 0 — 4y
0
Thank you very much! Great answer! User#34929 0 — 4y
Ad
Log in to vote
0
Answered by
haba_nero 386 Moderation Voter
4 years ago

You need to elaborate on the question more. I don't exactly know how to respond to this. If you are trying to select a random team, use this script:

local teams = {"Red","Blue","Green","Yellow","Orange"} --Your team colors
function SelectTeam()
    local chosenTeam = teams[1,#teams] --Selects a random team
    print(chosenTeam.." was chosen for this round.")
    return chosenTeam
end

You need to elaborate on the question in order for me to help

Best,

Haba_nero

Answer this question