Hi, I have this one humanoid with a range and one without a range. I want the first one to keep moving along a path until its range touches a humanoid, and once the humanoid is dead/out of range I want it to stop shooting and keep walking but I just can't get it to stop shooting and keep walking. It also doesn't stop sometimes.
local unit = script.Parent
local humanoid = unit:FindFirstChild("Humanoid")
local hrp = unit:FindFirstChild("HumanoidRootPart")
local waypoints = game.Workspace.Waypoints
local currentWaypoint = 0
local currentDest = nil
local range = unit.Range
local debounce = false
local away = false
range.Position = unit.HumanoidRootPart.Position
function onTouched(target)
local targethum = target.Parent:FindFirstChild("Humanoid")
if targethum then
away = false
humanoid:MoveTo(hrp.Position)
if not debounce then
if not away then
debounce = true
while targethum.Health > 0 do
print("shooting")
targethum.Health -= 20
wait(1)
end
end
debounce = false
end
end
end
local function onTouchEnded()
away = true
if currentDest ~= nil then
print("continue moving")
humanoid:MoveTo(currentDest)
end
end
function moving()
humanoid:MoveTo(waypoints[currentWaypoint].Position)
currentDest = waypoints[currentWaypoint].Position
print(currentDest)
humanoid.MoveToFinished:Connect(function(success)
if success and currentWaypoint < (#waypoints:GetChildren() - 1) then
currentWaypoint += 1
humanoid:MoveTo(waypoints[currentWaypoint].Position)
end
end)
currentDest = nil
--humanoid:MoveTo(waypoints[currentWaypoint].Position)
end
moving()
range.Touched:Connect(onTouched)
range.TouchEnded:Connect(onTouchEnded)