How would I ignore the local player in loop

I want this loop to ignore the local player and get everyone else.

It keeps getting everyone including the local player

I’ve tried different things but they just broke the script.

for _,v in pairs (game.Players:GetChildren()) do
			
			
			local Distance = (v.Character.Torso.Position-player.Character.Torso.Position).Magnitude
			if Distance < range then
				local victims = v
				move_type = "Crows"
				genjutsu:FireServer(move_type,victims)
			else

you can use this

if v ~= game.Players.LocalPlayer then
--//Do the stuff here
end
1 Like

Another fancy way to do it is, since this line exists:

local Distance = (v.Character.Torso.Position-player.Character.Torso.Position).Magnitude

If the current v was indeed the local player, then technically the distance between the local player and himself is gonna be 0. You can simply check if the distance is also ~= to 0.

if Distance < range  and Distance ~= 0 then
3 Likes

It’s better to compare player objects than their names. Comparing names is a string comparison which is slower than object comparison which checks if the references point to the same object.

Apart from the technical difference, the code is cleaner because logically: comparing player objects is enough, adding names doesn’t change anything in terms of the outcome.

1 Like

You should use an if statement instead, as said by @Hexlinee

In theory, this could think another player is the local player, if they were inside eachother.

for _,v in pairs (game.Players:GetChildren()) do

The Local Player is different for every Client, it would ignore everyone then?
What are you trying to do?

No, this is run in a Local Script, just from looking at the script, I can see it is some form of “move” or “attack”.

He asks how he can avoid that the player’s attack hits themselves.

  local players = game:GetService("Players")
  local victims = players:GetPlayers()

  I = table.find(victims, players.LocalPlayer)
  table.remove(victims, I)

   for _, player in ipairs (victims) do
				   genjutsu:FireServer("Crows", player)
   --  will fire the event for everyone other than the Local Player
   end

However it could be as simple as only firing for the Local Player and on the server, exempting him from the victims list.

I doubt that would happen, unless the game disables character cancolliding. But even if that’s the case, there definitely would be a very tiny friction, which would still not be equal to 0

I’m trying to make my character turn invisible to people within range when I activate this move. But the player that uses this move also turns invisible on their screen. This script goes to the server then back to a local script which is supposed to make the user invisible for players within the range.

My point is, that although it is extremely unlikely, it COULD happen. And on a seperate note, comparing distance to find if it’s the local player is simply unnecessary, it’s a waste of processing power and is just a wierd way to go about it.

Simply append an if statement to your for loop to ensure the player is not the LocalPlayer:

local Players = game:GetService("Players");
local LOCAL_PLAYER = Players.LocalPlayer;
...
for _,player in ipairs(Players:GetPlayers()) do --Always use :GetPlayers()
    if player ~= LOCAL_PLAYER then -Making sure the player is NOT the LocalPlayer
        --Your code here
    end
end