How to Infinitely Check if something = false?

Ok So heres my work.

  1. The Code Inserts Players in a Table if the IntroTag or the AFK is False.
    If one is true, it removes the player from the table.

  2. What im wanting to do is to infinitely check to see if a players IntroTag or AFK Value changes.

     local plrs = {}
     	
     	for i, player in pairs (game.Players:GetPlayers()) do
     		  if player.IntroTag.Value == false and player.AFK.Value == false then 
     			table.insert(plrs,player)--Add each players into plrs table
     			print(player)
     		elseif player.IntroTag.Value == true or player.AFK.Value == true then
     			warn(player, 'has Been Removed From Table - Intro Or AFK.')
     			for i = 1, #plrs do
     			if plrs[1] == Player then
     			table.remove(plrs,i) --Remove Player from table if they are in the intro or AFK.
     			
     			
     			end 
     		end
     	end
     end
     						
     print('Does Table have More than Two?')
     if #plrs >= 2 then
     		print('Yes it Does!')
     	else
     		repeat wait() until #plrs >= 2
     		print('Needed Players in Table: 2')
     	end
    

You can connect to the .Changed event of the IntroTag or AFK instances. This will fire your function passing the new value every time it changes.

example:

player.IntroTag.Changed:Connect(function(new_value)
    print(player.Name.."'s new IntroTag Value:"..tostring(player.IntroTag.Value))
    -- note: I like to index the Instance.Value instead of using the new_value parameter, as other properties can cause .Changed to fire. There's :GetPropertyChangedSignal(property) for this reason, too.
end)

4 Likes

Try using the .Changed (dammit, I got beat) events instead of endlessly running into an unnecessary loop. However, avoid changing plrs while the game is in progress and that can be done through a bool check. Due to my boredom, I have cleaned the lines but did not implement the .Changed events:

local plrs = game.Players:GetPlayers()
 	
for _, player in pairs(plrs) do
	if player.IntroTag.Value or player.AFK.Value then 
 		warn(player, 'has Been Removed From Table - Intro Or AFK.')
 		for i = 1, #plrs do
 			if plrs[i] == Player then
 				table.remove(plrs, i) --Remove Player from table if they are in the intro or AFK.
 			end
		end
	end
end

print('Does Table have More than Two?')
if #plrs >= 2 then
	print('Yes it Does!')
else
	repeat wait() until #plrs >= 2
	print('Needed Players in Table: 2')
end

Worst case scenario is that when the player does not have IntroTag or AFK created yet, they must be loading. Thus causes the game to stop due to the error at the line after the for loop.

1 Like

You can use a repeat wait() until true statement, however as Operatik mentioned, this could potentially result in an infinite loop - yielding your game. You should - at the very least - implement a max wait time when using repeat wait() until true… like this:

local MAX_WAIT_TIME = 5; -- wait up to 5 seconds before moving on.

local start_time = tick()
repeat
    wait()
until
    your_condition or (tick() - start_time >= MAX_WAIT_TIME)

if your_condition then -- our condition was met

else -- moved on because we waited too long.

end

I came up with this. Could this work Efficiently?

                    end 
				end
			end
				player.IntroTag.Value.Changed:Connect(function(new_value)
					if new_value == false then
					print(player.Name.." 's New IntroTag Value:"..tostring(player.IntroTag.Value))
					table.insert(plrs,player)
				else
					table.remove(plrs,i)	
		end
	end)
end

Correct me if I’m wrong, but don’t Values have a separate Changed event that only fires when the . Value changes?

Wiki

2 Likes

You are right - my mistake :slight_smile: What I said only applies to other Instances, not BaseValues.

Im using boolvalues and it works with others too

I’m not sure exactly where you’ve put that Changed handler, however if you have it embedded into a loop or something similar, make sure you are cleaning up the connection. Otherwise I don’t see why it would be inefficient.

Were it says --Loop Here-- Should i put something like: repeat wait(max_wait) until #plrs >= 2 ?
Or Should i Use while wait(max_wait) do ?

	        	local plrs = {}
	        	for i, player in pairs (game.Players:GetPlayers()) do
					  if player.IntroTag.Value == false and player.AFK.Value == false then 
	        			table.insert(plrs,player)--Add each players into plrs table
	        			print(player)
	        		elseif player.IntroTag.Value == true or player.AFK.Value == true then
						warn(player, 'has Been Removed From Table - Intro Or AFK.')
						for i = 1, #plrs do
						if plrs[1] == Player then
						table.remove(plrs,i) --Remove Player from table if they are in the intro or AFK.
						
						
						end 
					end
				end 
                  -- Loop Here? --
					player.IntroTag.Value.Changed:Connect(function(new_value)
						if new_value == false then
						print(player.Name.." 's New IntroTag Value:"..tostring(player.IntroTag.Value))
						table.insert(plrs,player)
					else
						table.remove(plrs,i)	
			end
		end)
	end