Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

For loop not printing when adding a player into a table?

Hello there. Currently, I am trying to make a system where a player joins the game, they have a bool value in their player, and when they press a button in the main menu GUI, the bool value goes away ( Don't worry I already have the bool value system that goes away when they press a GUI ) and they are added into a table in a script. But the problem is that when I want it to print something when a player joins the table, it doesn't print anything.

local contestants = {}

for _, v in pairs (game.Players:GetPlayers()) do

     if not v:FindFirstChild("CanPlay") then
          table.insert(contestants, v)
          print("A player has been added to the contestants") 
      end
end

Any answers are accepted. Thank you for your time. Stay safe. ShortTeamROBLOX

4 comments

0

Use .PlayerAdded

0

But thing is that if I use the playeradded event, then the function would only run when the player joins the game, lets say the player is afk in the main menu and they get teleported to the map, I don't want that to happen.

0

Let me just try .PlayerAdded to see if it works

0

The thing is that if I do PlayerAdded, it will look for when I join the game and it will stop afterwards. et me post the script I used.

1 answer

This is the script I used for the PlayerAdded:

local InMenu = {}
local contestants = {}

game:GetService("Players").PlayerAdded:Connect(function(plr)
	if plr:WaitForChild("CanPlay", 7) then
		table.insert(InMenu, math.random(), plr)
		warn("A Player has join the InMenu table")
	end		
					
	for _, v in pairs(game:GetService("Players"):GetPlayers()) do
		if not v:WaitForChild("CanPlay") then
			table.remove(InMenu, math.random())
			table.insert(contestants, math.random(), v)
			warn("A player has been added to the CanPlay table")
		end	
	end
end)

This only fires when I join the game though.

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