How would I ignore a part in a for i,v in pairs loop

I want to ignore the part if it belongs to the local player

The damage script gets the players local players torso and damages them when they punch.

I’ve try putting the players in a table and removing the local player but that caused more problems.

for i,v in pairs (game.Workspace:GetDescendants()) do
			if v:IsA("BasePart") and v.Name == "Torso" and v.Parent:FindFirstChild("Humanoid") then
				if (ClientTorso.Position - v.Position).Magnitude < 5 then
				
						v.Parent.Humanoid:TakeDamage(5)
					
				end
			end
		end

You are getiing the descendants of the workspace which is why it gets error you would need to fo it like this:

for i, v in pairs (game.Workspace:GetDescendants()) do
        if v:IsA("Model") then
	      for _, item in pairs(v:GetChildren()) do
                     if item:IsA("BasePart") and item.Parent:FindFirstChildWhichIsA("Humanoid") then
                            if (ClientTorso.Position - otherchar.PrimaryPart.Position).Magnitude > 5 then
                                  item.Parent:FindFirstChildWhichIsA("Humanoid"):TakeDamage(5)
                            end
                     end
              end
	end
end

I think it wold be better doing it this way:

local Players = game.Players:GetPlayers()

for _, player in pairs(Players) do
    if player == localPlayer then return end
	local otherChar = player.Character
	if otherChar then
		local dist = (ClientTorso.Position - otherChar.PrimaryPart.Position).magnitude
		if dist < 5 then
			local human = otherChar:FindFirstChild"Humanoid"
			if human then
				human:TakeDamage(5)
			end
		end
	end
end

To expand on the other responses, I think what I would do is this:

  • Scan through Players list instead
  • Use DistanceFromCharacter method on the local player
  • Use FindFirstChildOfClass to grab the humanoid
local localPlayer = game.Players.LocalPlayer
for i,player in ipairs(game.Players:GetPlayers()) do
	if player ~= localPlayer and player.Character and player.Character.PrimaryPart then
		if localPlayer:DistanceFromCharacter(player.Character.PrimaryPart.Position) < 5 then
			local hum = player.Character:FindFirstChildOfClass("Humanoid")
			if (hum) then
				hum:TakeDamage(5)
			end
		end
	end
end
6 Likes