Skip to content

ForEachPlayer

Calls a function for every connected player, in join order.

The loop of GetPlayers in one call. The list is taken before the first call, so the function may kick, hold or spawn players as it goes; a player who left meanwhile is skipped. With inWorld true only the players standing in the world are visited - the ones still loading or held by OnPlayerRequestSpawn are left out (IsPlayerInWorld). The function returning false stops the loop.

ForEachPlayer(fn [, inWorld])
Parameter Type
fn function function(pid) end; return false to stop
inWorld boolean true = the players in the world alone (false) (optional)

number - how many players the function saw

-- everyone in the world reads the line; the loading ones get theirs from OnPlayerSpawn
ForEachPlayer(function(pid)
ShowInfoText(pid, "The hunt begins", 3000)
end, true)
-- the first admin online
local admin
ForEachPlayer(function(pid)
if IsPlayerAdmin(pid) then admin = pid return false end
end)

GetPlayers · GetPlayerCount · IsPlayerInWorld · the Players group of the index