Hey guys! I’m trying to make an alpaca ai, but it seems to glitch the game. Does anyone know how to fix this?
while wait() do
if humanoid.Health > 0 then
if target and hasweapon(character) then
stop = true
spawn(function()
if target then
stop = false
goto(target.PrimaryPart.Position + Vector3.new(math.random(-3,3),0,math.random(-3,3)))
end
end)
spawn(function()
if target and hasweapon(character) and canswing and (target.PrimaryPart.Position-root.Position).Magnitude <= 8 then
canswing = false
swing:Play()
local weapon = getweapon()
local candmg = true
local hitalpacas = {}
local dmg = weapon.Blade.Touched:Connect(function(hit)
if hit.Parent.Parent == workspace.Alpacas and candmg and not table.find(hitalpacas, hit.Parent) then
candmg = false
table.insert(hitalpacas, hit.Parent)
local armor = hasarmor(hit.Parent)
if armor then
hit.Parent:FindFirstChild("Humanoid").Health -= weapon:GetAttribute("damage") * armor:GetAttribute("absorption")
armor:SetAttribute("durability", armor:GetAttribute("durability") - math.round(100/weapon:GetAttribute("damage")))
else
hit.Parent:FindFirstChild("Humanoid").Health -= weapon:GetAttribute("damage")
end
wait(.1)
candmg = true
end
end)
wait(.1)
canswing = true
dmg:Disconnect()
end
end)
if target.Humanoid.Health == 0 then
local nearalpacas = getnear()
if #nearalpacas > 0 then
target = nearalpacas[1]
else
target = nil
end
end
else
if hasweapon(character) then
local nearalpacas = getnear()
if #nearalpacas > 0 then
target = nearalpacas[1]
end
else
target = nil
end
if not target then
goal = Vector3.new(math.random(-100,100),0,math.random(-100,100))
goto(goal)
end
end
else
break
end
end