Nearest player not working under some circumstances?

Hi everyone! I made a script where it finds the nearest player from the local player’s humanoid root part. Here is the script

game.ReplicatedStorage.ShoFeatherAttack.OnServerEvent:Connect(function(player1)
	local char = player1.Character
	local humanoidrootpart = char.HumanoidRootPart
	
	local feathers = game.ReplicatedStorage.Feathers:Clone()
	
	local Players = game:GetService("Players")

	local maxDistance = 50

	local nearestPlayer, nearestDistance
	for _, player in pairs(Players:GetPlayers()) do
		local character = player.Character
		local distance = player:DistanceFromCharacter(humanoidrootpart.Position)
		if not player1 or player or character or char or
			distance > maxDistance or
			(nearestDistance and distance >= nearestDistance)
		then
			continue
		end
		nearestDistance = distance
		nearestPlayer = player
		print("The nearest player is ", nearestPlayer)
		
		humanoidrootpart.Position = nearestPlayer.Character.HumanoidRootPart.Position

		nearestPlayer.Character.Humanoid.Health = nearestPlayer.Character.Humanoid.Health - 25
	end
end)

Basically, once it finds the nearest player, it teleports you to the nearest players position and damages them. However, this only works for one player. When the other player tries it, they dont teleport, and it damages themselves. Any fixes for this? Tysm I appreciate it.

local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local FeatherAttack = RS:WaitForChild("ShoFeatherAttack")
local Feathers = RS:WaitForChild("Feathers")
local maxDistance = 50

FeatherAttack.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local hrp = char.HumanoidRootPart
	local FeathersClone = Feathers:Clone()
	local player, distance, nearestPlayer, nearestDistance = nil, math.huge, nil, math.huge
	
	for _, plyr in ipairs(Players:GetPlayers()) do
		player = plyr
		local character = player.Character
		distance = player:DistanceFromCharacter(hrp.Position)
		if plr == player and char == character then
			continue
		end
		if distance > maxDistance or distance >= nearestDistance then
			continue
		end
	end
	
	if nearestPlayer and nearestDistance < maxDistance then
		nearestDistance = distance
		nearestPlayer = player
		print("The nearest player is ", nearestPlayer)
		hrp.Position = nearestPlayer.Character.HumanoidRootPart.Position
		nearestPlayer.Character.Humanoid.Health -= 25
	end
end)

If a player’s Character property was nil when the check was ran, Player:DistanceFromCharacter (roblox.com) returns 0. You’ll want to check for the player’s character prior and see if they’ve one spawned in.

if player.Character ~= nil then
	local character = player.Character
	local distance = player:DistanceFromCharacter(humanoidrootpart.Position)

EDIT
Just as an added note, this will not work:

if not player1 or player or character or char or
			distance > maxDistance or
			(nearestDistance and distance >= nearestDistance)
		then

That is because not only does its magic with the first value, and not the entire thing – you may want to do something like this instead.

if not (player1 and player and character and char and distance > maxDistance and (nearestDistance and distance >= nearestDistance)) then