Why isnt for loop activating for every player?

Hello. I’ve got this code here where a gui is supposed to enable whenever the “killer” is within the distance of 10 of a “worker”

The Code:

local inputservice = game:GetService("UserInputService")
local rss = game:GetService('RunService')
local rs = game:GetService('ReplicatedStorage')
local Plrs = game:GetService('Players'):GetPlayers()
local xx = 0
local WithinDistanceKill = false
while true do	
	for i,Door in pairs(Plrs) do
		if game.Players.LocalPlayer:FindFirstChild('PlayerCriticalInformation').KillerIntValue.Value == true and Door:FindFirstChild('PlayerCriticalInformation'):FindFirstChild('WorkerIntValue').Value == true and Door.Character:FindFirstChild('Humanoid').Health ~= 0 and game.Players.LocalPlayer:FindFirstChild('PlayerCriticalInformation'):FindFirstChild('CanKill').Value == true then
			local Mag = (Door.Character.HumanoidRootPart.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Magnitude
			if Mag <= 10 and WithinDistanceKill == false then
				WithinDistanceKill = true
				game.Players.LocalPlayer.PlayerGui.KillGui.Enabled = true
				inputservice.InputBegan:connect(function(i,g)
					if not g then
						if i.KeyCode == Enum.KeyCode.E then
							for ii,Door1 in pairs(Plrs) do
								if WithinDistanceKill == true and Door1.Name ~= game.Players.LocalPlayer.Name and Door1.Character.Humanoid.Health ~= 0 and game.Players.LocalPlayer:FindFirstChild('PlayerCriticalInformation'):FindFirstChild('CanKill').Value == true then
									game.Players.LocalPlayer:FindFirstChild('PlayerCriticalInformation'):FindFirstChild('CanKill').Value = false
									rs.InGame.KillEvent:FireServer(Door1)
									game.Players.LocalPlayer.PlayerGui.KillGui.Enabled = false
									WithinDistanceKill = false
									wait(10)
									game.Players.LocalPlayer:FindFirstChild('PlayerCriticalInformation'):FindFirstChild('CanKill').Value = true
									
								end		
							end	
						end
					end	
				end)
			elseif Mag > 10 and WithinDistanceKill == true then
				WithinDistanceKill = false
				game.Players.LocalPlayer.PlayerGui.KillGui.Enabled = false
			end
			
		end
	end
	wait()
end

Now it does work, but not for every player.
Problem Vid


As you can see, the GUI shows up for one player, but not the other. They have the same values, so it isn’t that type of problem.
Just wondering where I went wrong. I’m assuming it’s something to do with the for loop with the Plrs.

Any help is appreciated!

I’m not sure, but i think you should get the game.Players:GetPlayers() everytime it loops, because it only gets the players when you start, so players who join later don’t get added to that list.

If you place the Plrs inside the while loop, it should work. Please reply if it doesn’t

Try this.

game.Players.PlayerAdded:Connect(function(
    Plrs = game:GetService("Players"):GetPlayers()
end)

This will update your player list whenever a new player joins. you’ll want to update it whenever a player leaves by using

game.Players.PlayerRemvoing:Connect(function(
    Plrs = game:GetService("Players"):GetPlayers()
end)

I didn’t read this post so please correct me if I’m wrong…

Ok so if you want to create a loop that does something to every player then I suggest you do this:

for _, player in pairs(game.Players.GetPlayers()) -- for every player in the game...
        -- do stufffffff
end) -- end this 'for' loop