local function moveTo(humanoid, targetPoint, andThen)
local targetReached = false
-- listen for the humanoid reaching its target
local connection
connection = humanoid.MoveToFinished:Connect(function(reached)
targetReached = true
connection:Disconnect()
connection = nil
if andThen then
andThen(reached)
end
end)
-- start walking
humanoid:MoveTo(targetPoint)
-- execute on a new thread so as to not yield function
task.spawn(function()
while not targetReached do
-- does the humanoid still exist?
if not (humanoid and humanoid.Parent) then
break
end
-- has the target changed?
if humanoid.WalkToPoint ~= targetPoint then
break
end
-- refresh the timeout
humanoid:MoveTo(targetPoint)
task.wait(6)
end
-- disconnect the connection if it is still connected
if connection then
connection:Disconnect()
connection = nil
end
end)
end
local function MoveMob(newMob, map)
if not newMob or not newMob.Parent then return end
local humanoid:Humanoid = newMob:FindFirstChildOfClass("Humanoid")
local root = newMob:FindFirstChild("HumanoidRootPart")
if not humanoid or not root then return end
local waypointsFolder = map:FindFirstChild("Waypoints")
if not waypointsFolder then return end
local waypoints = waypointsFolder:GetChildren()
table.sort(waypoints, function(a, b)
return tonumber(a.Name) < tonumber(b.Name)
end)
for _,waypoint in ipairs(waypoints) do
newMob.MovingTo.Value = tonumber(waypoint.Name)
moveTo(humanoid, waypoint.Position)
humanoid.MoveToFinished:Wait()
end
-- doszedł do bazy
if map:FindFirstChild("Base") and map.Base:FindFirstChild("Humanoid") then
map.Base.Humanoid:TakeDamage(humanoid.MaxHealth)
end
newMob:Destroy()
end
** You are now Level 5! **