Getting all players and do something

Hello I want to make a command like :bring all but IDK how to get all players.

2 Likes

you can loop through game.Players like this

-- THIS IS PSEUDO CODE IT WILL NOT WORK OUT OF THE BOX
for index, player in ipairs(game.Players:GetDescendants()) do
    -- Move the player here
end

local targetCFrame = CFrame.new(Vector3.new(0, 0, 0)) – set this to the character whos using the commands cframe
for i, v in next, game:GetService(“Players”):GetPlayers() do
local primaryPart = v.Character and v.Character.PrimaryPart
if primaryPart then
v.Character:SetPrimaryPartCFrame(targetCFrame)
end
end

Your code wouldn’t work because your getting descendants, meaning you would be getting the backpack the PlayerGui etc.

1 Like

You can use this function to teleport all players in the area to the desired player.

local plrs = game:GetService("Players")

-- Call this function when the command is sent
local function bringPlayers(playerWhoSentCommand) -- This variable should be specified every time this function is called, or else this wont work.
	local playerWhoSentCmdChar = playerWhoSentCommand.Character or playerWhoSentCommand.CharacterAdded:Wait()
	for i, p in pairs(plrs:GetChildren()) do
		if p.Name == playerWhoSentCommand.Name then
		else
			local characterToTeleport = p.Character or p.CharacterAdded:Wait()
			characterToTeleport:MoveTo(playerWhoSentCmdChar:FindFirstChild("HumanoidRootPart").Position)			
		end
	end
end

-- Example event to bring players
workspace.BringPart.Touched:Connect(function(hitPart)
	if hitPart.Parent:FindFirstChild("Humanoid") then
		local player = plrs:GetPlayerFromCharacter(hitPart.Parent) -- Gets player object from part that touches the teleport brick
		bringPlayers(player) -- Fires the function with the player to teleport others to
	end
end)

Hope this helps!

wait(5) -- Change this to however you like

Offset = Vector3.new(0,2,0) -- Our Offset

for _, Player in pairs(game.Players:GetPlayers()) do -- Gets Players
	Player.Character:MoveTo(workspace.Funni.Position + Offset) -- Teleports to Position + Out Offset, Random Example inside
	
	print("Teleported")
	break -- Breaks the Loop, Remove if you like
end

Here’s a very simple example as to how you would do this:

local PS = game:GetService("Players")

PS.PlayerAdded:Connect(function(player)
	local char = player.Character or player.CharacterAdded:Wait()
	
	player.Chatted:Connect(function(message)
		if message == ":bring all" then
			for _, target in ipairs(PS:GetPlayers()) do
				local targetChar = target.Character or target.CharacterAdded:Wait()
				
				targetChar:PivotTo(char:GetPivot())
			end
		end
	end)
end)

Create a table and a chat connection that updates when players join and leave.

When function is called, bring all players to a specific location based on table.

Chat command is :bringall

If you copy this into a script and place it in ServerScriptService, by chatting the command :bringall all players in the game should be moved to Vector3.new(0,5,0)

local Players = game:GetService("Players")

local playersInGame = {}
local chatConnections = {}

local CHAT_COMMANDS = {
	[":bringall"] = function ()
		local bringToPosition = Vector3.new(0,5,0) -- Put your target location here: Eg. workspace.SpawnLocation.Position

		for plrName, playerObject in pairs (playersInGame) do
			if playerObject.Character and playerObject.Character:FindFirstChild("HumanoidRootPart") then
				playerObject.Character.HumanoidRootPart.Position = bringToPosition
			else
				warn ("Character not yet loaded for: "..plrName)
				continue
			end
		end
	end
}

Players.PlayerAdded:Connect(function(player)
	playersInGame[player.Name] = player
	
	chatConnection[player.Name.."Connection"] = player.Chatted:Connect(function(message)
		if CHAT_COMMANDS[message] then
			CHAT_COMMANDS[message]()
		end
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	if playersInGame[player.Name] then
		playersInGame[player.Name] = nil
		chatConnection[player.Name.."Connection"] = nil
	end
end)