If statement stoping a loop

I changed the loop to this:

local SpecList = {}
game.Players.ChildAdded:Connect(function()
	local Players = game.Players:GetPlayers()
	print(Players)
	print("Outside")
	for i, v in ipairs(Players) do
		print(SpecList)
		local Value = v.Character:WaitForChild("Value").Value
		local position = table.find(SpecList, v)
		if Value ~= 2 and v.Name ~= Player.Name and position == nil then
			table.insert(SpecList, v)
		end
	end
end)

A new issue has arose where nothing is being added to the array.

Are you trying to make a spectation system? What exactly is value ~= 2?

I don’t really know about the Value thing. if its value doesn’t change (so if it was 0 once it will always be 0), then you could do this:

local SpecList = {}
game.Players.ChildAdded:Connect(function(newPlr)
	if newPlr:IsA("Player") then	
		local newChr = newPlr.Character or newPlr.CharacterAdded:Wait()
		local Value = newChr:WaitForChild("Value").Value
		if Value ~= 2 and newPlr.Name ~= Player.Name  then
			table.insert(SpecList, newPlr)
		end
	end
end)

That’s checking if the player is or is not currently spectating so someone can’t spectate someone currently spectating.

so why not use a boolValue for that?

The player can have 3 states not just 2.

I cleaned up your code a little bit, see if this works now.

local SpecList = {}
game.Players:PlayerAdded:Connect(function()
	local Players = game.Players:GetPlayers()
	print(#Players)
	print("Outside")
	for i, v in pairs(Players) do
		print(SpecList)
      if v then
		local Value = v.Character:WaitForChild("Value").Value
		local position = table.find(SpecList, v)
		if Value ~= 2 and v.Name ~= Player.Name and position == nil then
			table.insert(SpecList, v)
		end
      end
	end
end)

The issue wasn’t fixed with that. For some reason with both versions, “Outside” is not being printed despite “{}” being printed.

local SpecList = {}
game:GetService("RunService").Heartbeat:Connect(function()
	for _,plr in game.Players:GetPlayers() do	
		local char = plr.Character or plr.CharacterAdded:Wait()
		local Value = char:WaitForChild("Value").Value
		if Value ~= 2 and plr.Name ~= Player.Name and not table.find(SpecList,plr) then
			table.insert(SpecList, plr)
		end
	end
end)

That worked! Thanks everyone for the help.

This will explode the client’s memory

Is there a way to do this without exploding the memory?

The easiest fix is to clear the table before re-populating it, or build a new table each time you try to switch perspectives. You can take inspiration from my more advanced spectation system:
https://github.com/Ziffixture/Roblox/blob/main/Spectate.lua

It should take very little code to modify the getTrackedCharactersInWorkspace function to reject characters who are currently spectating

I figured out how I can do what I wanted without exploding the memory I don’t think. I split the loop into 2 parts. Here’s what it looks like now:

local SpecList = {}
local Players = game.Players:GetPlayers()
for i, v in ipairs(Players) do
	print(SpecList)
	print("Outside")
	local Value = v.Character:WaitForChild("Value").Value
	if Value ~= 2 and v.Name ~= Player.Name then
		print("Inside")
		table.insert(SpecList, v)
	end
end

game.Players.PlayerAdded:Connect(function()
	local Players = game.Players:GetPlayers()
	for i, v in ipairs(Players) do
		print(SpecList)
		print("Outside2")
		local Character = v.Character or v.CharacterAdded:Wait()
		local Value = Character:WaitForChild("Value").Value
		local position = table.find(SpecList, v)
		if Value ~= 2 and position == nil then
			print("Inside2")
			table.insert(SpecList, v)
			print(SpecList)
			local Pos = table.find(SpecList, Player)
			if Pos ~= nil then
				table.remove(SpecList, Pos)
			end
		end
	end
end)

I had to add the part with removing the player because attempting to detect if the player was the one being checked would prevent anything from being added.

this script removes the player from SpecList if the value changes (I assume you want that) and it doesn’t use RunService:

local SpecList = {}

local function func(plr:Player)
	if plr.Name ~= Player.Name then
		local char = plr.Character or plr.CharacterAdded:Wait()
		local value:NumberValue = char:WaitForChild("Value")
		
		if value.Value ~= 2 then
			table.insert(SpecList, plr)
		end
		
		value.Changed:Connect(function()
			if value.Value == 2 then
				local pos = table.find(SpecList, plr)
				if pos then
					table.remove(SpecList, pos)
				end
			else
				table.insert(SpecList, plr)
			end
		end)
	end
end

for _,plr in game.Players:GetPlayers() do
	func(plr)
end

game.Players.PlayerAdded:Connect(function(plr)
	func(plr)
end)