I am using a script to make the NPC attack the player. And it is currently not working. I don't know why because I am a fairly new scripter. ``` local larm = script.Parent:FindFirstChild("Left Arm") local rarm = script.Parent:FindFirstChild("Right Arm")
function findNearestTorso(pos) local list = game.Players:GetPlayers() local torso = nil local dist = 10 local temp = nil local human = nil local temp2 = nil for x = 1, #list do temp2 = list[x] if temp2.Character:IsA("Model") then temp = temp2.Character:FindFirstChild("Torso") human = temp2.Character:FindFirstChild("Humanoid") if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then if (temp.Position - pos).magnitude < dist then torso = temp dist = (temp.Position - pos).magnitude end end end end return torso end
while true do
wait(0.1)
local target = findNearestTorso(script.Parent.Torso.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
end
That is my first script which I placed in the humanoid section and it keeps. My damage script is below
script.Parent.Touched:connect(function(part)
if part.Parent:FindFirstChild("Humanoid") then
part.Parent.Humanoid:TakeDamage(30)
end)
```