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 script choose the same Teams over and over again?

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

✓ Accepted answer

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

3 comments

0

Oh. So that's what she/he tried to do! Great answer!

0

,

0

Thank you very much! Great answer!

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

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