so basically im trying to make an npc that follows and attacks you, and it follows you, but it will not attack for some reason, this is the code and the part that isnt working is listed, and its not even giving me errors it just wont work
-- misc vars
local human = script.Parent.Noobanoid
local torso = script.Parent.Torso
-- stats
local config = script.Parent.Config
local health = config.Health.Value
local damage = config.Damage.Value
local speed = config.Speed.Value
local damageSpeed = config.DamageSpeed.Value
local range = config.Range.Value
human.Health = health
human.MaxHealth = health
human.WalkSpeed = speed
-- anim vars
swing = script.Parent.Swing
swingAnim = human:LoadAnimation(swing)
swingAnim.Priority = Enum.AnimationPriority.Action
-- sound vars
local punchSounds = {"rbxassetid://1193237596", "rbxassetid://1033173157", "rbxassetid://131237241", "rbxassetid://3064939784"}
local deathSounds = {"rbxassetid://2691819167", "rbxassetid://4494920504", "rbxassetid://481775819", "rbxassetid://566988981", "rbxassetid://856379539"}
function locateTorso(pos)
local list = workspace:children()
local torso = nil
local dist = range
local temp = nil
local human2 = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("Torso")
human2 = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human2 ~= nil) and (human2.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 = locateTorso(script.Parent.Torso.Position)
if target ~= nil then
human:MoveTo(target.Position, target)
end
end
-- damage code that dosent work
function attack()
local target = locateTorso(script.Parent.Torso.Position)
local human2 = target.Humanoid
if (human ~= nil) then
if (torso.Position - target.Position).magnitude < 5 then
human:TakeDamage(damage)
end
wait(damageSpeed)
end
end
