How to reference OTHER players in workspace

Basically I want to teleport an object to every player in the workspace except me. How would I reference others? I started the code and it works successfully(heres a bit of it:) but it also teleports the object to me as well.

local Player = game.Players.LocalPlayer
local Character = workspace:WaitForChild(Player.Name)
local Mort = Character.HumanoidRootPart.CFrame

You could just get all the players through a loop using: for i, v in pairs & just implementing a conditional check if the character is not equal to every other player:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Mort = Character:WaitForChild("HumanoidRootPart").CFrame

for _, OtherPlayer in pairs(game.Players:GetPlayers()) do
    local OtherChar = OtherChar.Character
    if OtherChar ~= Character then
        --Teleport your object here
    end
end

Do keep in mind however, that these changes will only be made on the client-side, if you’d want it so that every player can see the object, you can use RemoteEvents for that

2 Likes

Another way you can also do it

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Mort = Character:WaitForChild("HumanoidRootPart").CFrame

local players = game.Players:GetPlayers()

table.remove(players, table.find(players, Player))

for _, OtherPlayer in pairs(players) do
    local OtherChar = OtherPlayer.Character
	--Teleport object
end

Get the players, and then remove the index at which your player is in and then loop through, saves checking every player’s character

1 Like