Hi, i’m working in a mob system, Very basic, If the player is too far the mob will follow it, else if the player is too close it get damaged.
But there’s is a problem that makes it bad:
while task.wait() do
for i, v in pairs(workspace:GetChildren()) do
if v:IsA('Model') then
if game.Players:FindFirstChild(v.Name) then
local dist = (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).Magnitude
if dist <= 30 then
script.Parent.Humanoid:MoveTo(v.HumanoidRootPart.Position)
elseif dist <= 15 then
script.Parent.Humanoid:MoveTo(script.Parent.HumanoidRootPart.Position)
end
end
end
end
end
Humanoid:MoveTo (roblox.com)
looking through the documentation states there’s an 8-second delay before MoveTo() ends if the humanoid doesn’t reach its destination
local function Move(humanoid:Humanoid, targetPoint:Vector3)
local targetReached = false
-- listen for the humanoid reaching its target
local connection
connection = humanoid.MoveToFinished:Connect(function(reached)
targetReached = true
connection:Disconnect()
connection = nil
end)
-- start walking
humanoid:MoveTo(targetPoint)
-- execute on a new thread so as to not yield function
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)
wait(6)
end
-- disconnect the connection if it is still connected
if connection then
connection:Disconnect()
connection = nil
end
end)
end
while task.wait() do
for i, v in pairs(workspace:GetChildren()) do
if v and v:IsA("Model") and game.Players:FindFirstChild(v.Name) then
local distance
= (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).Magnitude
if distance <= 30 then
Move(script.Parent.Humanoid, v.HumanoidRootPart.Position)
end
end
end
end