Trying to get all players from a local script except the local player.
I have tried getting all players then doing an if statement but that wasn’t working.
Trying to get all players from a local script except the local player.
I have tried getting all players then doing an if statement but that wasn’t working.
local plr = game.players.localplayer
local others = {}
for _, otherPlayer in pairs(game.players:GetPlayers()) do
if otherPlayer ~= plr then
table.insert(others,otherPlayer)
end
end
Can’t you just do this?
local players = game:GetService("Players")
local playersTable = players:GetPlayers()
local plr = players.LocalPlayer
for i,player in pairs(playersTable) do
if player.Name ~= plr.Name then continue end
table.remove(playersTable,i)
break
end
print(playersTable) -- will print all players besides local player
Gets the players in a variable, loop through the table, and remove the index that has to be removed. Not sure if if player ~= plr then continue end
would do the same
edit: okay how did my brain forget that table.find
existed for a second
local Players = game:GetService'Players'
local LocalPlayer = Players.LocalPlayer
local OtherPlayers = Players:GetPlayers()
table.remove(OtherPlayers, table.find(OtherPlayers, LocalPlayer))
print(OtherPlayers)
I am currently getting just a “Table: 000000000x82B”
Alright, alright…
local Players = game:GetService("Players")
local function DoSomethingWithNotALocalPlayer()
for _, Player in ipairs(Players:GetPlayers()) do
if not Player == Players.LocalPlayer then
print(Player.Name .. " is not the local player!!!1!!1!!!!1!1")
-- Do something with the non-local player
end
end
end
Players.PlayerAdded:Connect(DoSomethingWithNotALocalPlayer) -- when player gets added, it goes through all the players and prints the name of all the other players. Of course, you could use this function in any other way, just by calling DoSomethingWithNotALocalPlayer().
If you want to print all the players in the table then you can iterate the table with a for loop like so:
for i, Player in pairs(OtherPlayers) do
print(Player)
end
nope, doesnt work. tried it and didn’t do anything.
I know that already.
3O Player.Character
Thanks, it works! It prints all players names.
Yeah, that’s how all tables are supposed to work.
for example:
t={"Apple","Banana","Pear"}
for index,value in ipairs(t) do
print(index,value)
end
-- Output:
--[[
1 Apple
2 Banana
3 Pear
]]
ipairs
only supplys the index (1st,2nd,3rd position in table etc…), but pairs supplies a string (for example {[“Key”]=value}) would print 1 for ipairs
. but “Key” for pairs.