[Easy Fix] Function problem

I have had a lot of trouble trying to loop through the table of players and print out a certain Team Name, It works by seeing if the players index value is even or Odd. For some-reason It dosen’t do anything.
Here is the Code:


local function teamUp() --Function to Change Team

for i, v in pairs(players) do --loop through table

   if i % 2 == 0 then --if even number

      print("Blue Team") --Will Be a Team

   else 
   	print("Red Team")	--Will Be a Team
   end
   end
   end
   
   while true do
   	wait(1)
   	print("Hi") -- Hi  does Print
   	teamUp() --Should Print Blue Team or Red Team
   end

I am a beginner programer and I would Really appreciate your help if you need more details let me know!

Did you define your Variable players as?:

local players = game:GetService("Players")

No. I haven’t done that but I will right now.

It looks like this now


local players = game:GetService("Players")
local player = game.Players:GetPlayers()

local function teamUp() --Funtion to Change Team

for i, v in pairs(player) do --loop through table

    if i % 2 == 1 then --if even number

       print("H") --Will Be a Team

    else 
		print("M")	--Will Be a Team
	end
	end
	end
	
	while true do
		wait(1)
		print("Hi")
		teamUp() --Should Print H or M
		print(teamUp)
	end

You should do for i, v in pairs(game.Players:GetChildren()) do. This will make it so that every time you call the teamUp function, it will loop through the current player list.

1 Like