local model = script.Parent
local humanoid = model.Humanoid
local target = model.Target
local hrp = model.HumanoidRootPart
local config = model.Configuration
local animations = model.Animations
local idleAnim = humanoid.Animator:LoadAnimation(animations.Idle)
local moveAnim = humanoid.Animator:LoadAnimation(animations.Move)
idleAnim:Play()
local function GetDistance(part1, part2)
if part1 and part2 then
return (part1.Position * Vector3.new(1,0,1) - part2.Position * Vector3.new(1, 0, 1)).Magnitude
else
return math.huge
end
end
function GetTarget(Character, distance)
for i, p in pairs(workspace:GetChildren()) do
if p:IsA("Model") and game:GetService("Players"):GetPlayerFromCharacter(p) then
if GetDistance(p.PrimaryPart, Character.PrimaryPart) < distance and p.Humanoid.Health > 0 then
return p
end
end
end
return nil
end
local function Running(speed)
if speed > 0 and not moveAnim.IsPlaying then
moveAnim:Play()
elseif speed <= 0 then
moveAnim:Stop()
end
end
humanoid.Running:Connect(Running)
local function isReached(humTarget, hrpTarget)
return target.Value and humTarget.Health > 0 and humanoid.Health > 0 and GetDistance(hrpTarget, hrp) < config.Distance.Value
end
local function GoToTarget(target)
local hrpTarget = target:WaitForChild("HumanoidRootPart")
local humTarget = target.Humanoid
while isReached(humTarget, hrpTarget) do
if GetDistance(hrpTarget, hrp) > config.AttackDistance.Value then
humanoid:MoveTo(hrpTarget.Position)
end
wait()
end
end
local function Idle()
while target.Value == nil do
wait(math.random(4, 10))
humanoid:MoveTo(hrp.Position + Vector3.new(math.random(-10, 10), 0, math.random(-10, 10)))
target.Value = GetTarget(model, config.Distance.Value)
end
end
while humanoid.Health > 0 do
wait()
if target.Value then
GoToTarget(target.Value)
target.Value = nil
elseif target.Value == nil then
Idle()
end
end
wait(3)
model:Destroy()