Using For Loops

Hello.

I have this game here that is going to have a voting system.
So how it works is all the players in the team “Playing”, has this GUI with labels. What I’m trying to accomplish is having all the Players that are in the team, the “Playing” team, having their name put on a label. Picture of how it looks.
Anteckning 2020-06-29 002835
Now what I’m struggling with is how to perform this task. I have under each label this:
Anteckning 2020-06-29 002956
The Occupied is a bool value, meant to store if the label was occupied by a player already, and the Plr is an object value meant to store the player itself. I was grasping for straws when I used those two objects, but my plan was to:

  1. Use a for loop for each label, which then checks if it is occupied, if not it chooses a player and assigns it to the Plr variable. Now my problem was how do I choose the player? I can’t use a for loop, because then it will just go through all the players and chose the last one, and I can’t either use math. random, as a player can be selected twice.

So my question is how do I iterate or loop through all the players without a playing being chosen twice?

I can provide a more detailed explanation if you require it. I’m not asking for a complete script, just a start-off point! Thanks :slight_smile:

You can do the last option of the math.random but store that random in a variable, right after you check over the players using that variable and if the value of Plr is the player then it should check over again.

Example:


local randomPlayerIndex = math.random(1, #game.Players:GetPlayers())
local player = game.Players:GetPlayers()[randomPlayerIndex]
repeat
          randomPlayerIndex = math.random(1, #game.Players:GetPlayers())
          player = game.Players:GetPlayers()[randomPlayerIndex]
until player ~= Plr.Value

Occupied.Value = true
Plr.Value = player

1 Like

@AstralBlu_e Thanks for the reply.
Your idea works, but how would I check the for the other Plr variables in the other labels?

Its actually simple, Inside the for loop you would have to do this:

-- I will assume you are doing for i ,v 

for i,v in pairs(TheGUI:GetChildren()) do
      -- TheGUI is the parent of all these text labels
      local Occupied = v.Occupied
      local Plr = v.Plr
      -- From now on it will work normally
      end
end

@AstralBlu_e Yes, but wouldn’t this only repeat inside the each label, not the whole. What I currently have in a ServerScript

local function DiscussionCompleted(Plr)
	TeamPlaying = PlayingTeam:GetPlayers()
	
	for i,v in pairs(Plrs:GetPlayers()) do
		
		if v.Team == TeamPlaying then
			
			for ii,vv in pairs(v.PlayerGui.VotingWIP.Frame:GetChildren()) do
				
				local randomPlayerIndex = math.random(1, #TeamPlaying)
				local player = TeamPlaying[randomPlayerIndex]
				local Occupied = vv.Occupied
				local Plr2 = vv.Plr
				
				repeat
					randomPlayerIndex = math.random(1, #TeamPlaying)
					player = TeamPlaying()[randomPlayerIndex]
				until player ~= Plr2.Value		
					Occupied.Value = true
					Plr2.Value = player
			end
			RS.InGame.VotingBegin:FireClient(TeamPlaying)			
		end
	end
end

That piece of code is currently not changing any "Occupied" or "Plr" value under the labels.

Its because you did player = TeamPlaying()[randomPlayerIndex]

game.Players:GetPlayers() returns a table therefore you shouldve done TeamPlaying[randomPlayerIndex]

And you did :FireClient(TeamPlaying), TeamPlaying is a table therefore it wont work
You need to specify which client you are wanting to fire.

You should learn more about using remotes and tables.