For Loop Not Working

For some reason this for loop is not running. When I put a print before the for loop it will print, but if I put a print after the for loop it will not print.

local status = script.Parent.Parent:FindFirstChild("Status")

local plrs = {}

while true do 
	
	if status.Value == true then
		script.Parent.Touched:Connect(function(hit)
			local player = hit.Parent
			if player then

				for i, player in pairs(plrs) do
					print("hello sir ")
						if not player == player then
							table.insert(plrs,player)
							game.Workspace:WaitForChild(player.Name).Head.BillboardGui.Enabled = true
					
					end
				end
			end
		end)
	end
	wait(1)
end

Use the output. Maybe some object or property your referencing doesn’t exist and it breaks the code midway through.
But

  • You’re using a while loop, not a for loop. A for loop uses an iterator function or range.
  • You’re connecting a touched event every second. These exist indefinitely until the part is destroyed or the developer disconnects them. So after a minute, if anything touches the part its going to run that connected function 60 times.
    • Your code doesn’t need a loop, just check if the value is true inside the touched function.
  • if not player == player then
    • Okay this is always false. This is equal to saying (not player)==player → false==player → false.
    • But generally just don’t compare a variable with itself
1 Like

plrs is an empty table

its like doing:

for i,v in pairs({}) do
    print("hi")
end

so of course it wont print anything

1 Like

Yeah so I added this in and it still didn’t work, it only worked for the first player that hit the part

 local status = script.Parent.Parent:FindFirstChild("Status")

local plrs = {}

while true do 
	
	if status.Value == true then
		script.Parent.Touched:Connect(function(hit)
			local player = hit.Parent
			if player then
				if #plrs == 0 then
					table.insert(plrs,player)
					game.Workspace:WaitForChild(player.Name).Head.BillboardGui.Enabled = true
				else
				
				for i, player in pairs(plrs) do
					print("hello sir ")
						if not player == player then
							table.insert(plrs,player)
							game.Workspace:WaitForChild(player.Name).Head.BillboardGui.Enabled = true
						end
					end
				end
			end
		end)
	end
	wait(1)
end

What are you even trying to achieve? Billboard gui above the player’s head whenever he touches a part?

Yes, that and I want to add them to a table.

First of all, you should take @greatgavin advices.

In addition to @greatgavin’s post:

  • You’re naming the player = hit.Parent variable the same as your loop variable.

  • You’re assuming hit.Parent is a character - what if hit is just a random part?

  • game.Workspace:WaitForChild(player.Name) can just be replaced with hit.Parent, right?

  • Your table loop logic is wrong (aside from the bad variable name) – you should loop through all the players first to check if the player’s already in the list, then if they’re not, only then add them. Like:

    -- ...
    local found = false
    
    for _, plr in pairs(plrs) do
        if (player == plr) then
            found = true
            break
        end
    end
    
    if (not found) then
        table.insert(plrs, player)
    end
    -- ...
    

Most of all,

Please describe what you’re trying to do, instead of just saying “this code is broken”

if not player == player then

What? Nothing makes sense here…

 script.Parent.Touched:Connect(function(Hit)
  local Character = Hit.Parent
  if Character:FindFirstChildWhichIsA("Humanoid") then
    Character:WaitForChild("Head").BillboardGui.Enabled = true
  end
end

Your use of the table doesn’t even make sense, I can’t even understand what you’re trying to accomplish here.