i need the npc (where the script is in) deal 15 dmg to the closest player to it but i want it to also have a range of 3 studs and play anim on attack but it doesnt seem to work
local attackAnimationId = "rbxassetid://15561118444"
local attackRange = 2
local damageAmount = 15
function findNearestPlayer(pos)
local players = game.Players:GetPlayers()
local nearestPlayer = nil
local minDistance = attackRange + 1 than attackRange
for _, player in ipairs(players) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local distance = (player.Character.HumanoidRootPart.Position - pos).magnitude
if distance < minDistance then
nearestPlayer = player.Character.Humanoid
minDistance = distance
end
end
end
return nearestPlayer, minDistance
end
while true do
wait(3)
local nearestPlayer, distance = findNearestPlayer(script.Parent.HumanoidRootPart.Position)
if nearestPlayer and distance <= attackRange then
nearestPlayer.Health = nearestPlayer.Health - damageAmount
local humanoid = script.Parent:FindFirstChild("Humanoid")
if humanoid and humanoid:IsA("Humanoid") then
humanoid:LoadAnimation(attackAnimationId):Play()
end
end
end```
please place your code like this
** You are now Level 1! **