So I want to compare the array players (which is basically the old players) with the new array I get by game:GetService("Players"):GetChildren()
The problem here is that I got two for loops. If players contains two players, and a new player joins, then game:GetService("Players"):GetChildren() returns an array with 3 players in it. So newPlayer is, let's say 'Player1', so oldPlayer should also be 'Player1' then.
Now it checks if oldPlayer is not newPlayer, which is false, but then newPlayer is still 'Player1' and oldPlayer is now 'Player2'. So oldPlayer is not newPlayer is true now.
Also, I am 'Player4', so it inserts 'Player2' into the table players which it shouldn't, because 'Player2' is an old player.
I need another way to compare these arrays...
local players = {}
local playerNames = {}
local localCharacter = nil
local toPlayerCFrame = CFrame.new(0, 0, 0)
local function getPlayerNames()
for _, newPlayer in pairs(game:GetService("Players"):GetChildren()) do
for _, oldPlayer in pairs(players) do
if oldPlayer.Name ~= newPlayer.Name and oldPlayer.Name ~= game:GetService("Players").LocalPlayer.Name then
table.insert(players, newPlayer)
table.insert(playerNames, newPlayer.Name)
end
end
end
localCharacter = game:GetService("Players").LocalPlayer.Character
end