Hi, I was wondering if you guys knew of a way I could make my monster follow a character but not close the gap between the two at a certain distance (not sure what the distance is going to be yet but not far). The goal I am trying to reach is to make it go towards the character then stop at the distance, but it won't run away from the player when they are the ones walking at it. Is there a way to do this?
This is the current following script that I am working with right now. If you need any other information don't hesitate to ask. :) Thank you, Peanutthedestoyer
local larm = script.Parent:FindFirstChild("Left Arm") local rarm = script.Parent:FindFirstChild("Right Arm") function findNearestTorso(pos) local list = game.Workspace:children() local torso = nil local dist = 20 --this is the distance your monster will see you local temp = nil local human = nil local temp2 = nil for x = 1, #list do temp2 = list[x] if (temp2.className == "Model") and (temp2 ~= script.Parent) then temp = temp2:findFirstChild("Torso") human = temp2:findFirstChild("Humanoid") if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then if (temp.Position - pos).magnitude < dist then torso = temp dist = (temp.Position - pos).magnitude end end end end return torso end while true do wait(0.1) local target = findNearest(PlayerTorso(script.Parent.Torso.Position) if target ~= nil then-- script.Parent.Humanoid:MoveTo(target.Position, target) end end
local larm = script.Parent:FindFirstChild("Left Arm") local rarm = script.Parent:FindFirstChild("Right Arm") distanceStopped = 10 --Distance where the monster stops moving function findNearestTorso(pos) local list = game.Workspace:GetChildren() local torso = nil local temp = nil local dist = 100 --this is the distance your monster will see you local human = nil local temp2 = nil for x = 1, #list do temp2 = list[x] if (temp2.className == "Model") and (temp2 ~= script.Parent) then temp = temp2:findFirstChild("Torso") human = temp2:findFirstChild("Humanoid") if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then if (temp.Position - pos).magnitude < dist then torso = temp dist = (temp.Position - pos).magnitude end end end end return torso end while true do wait(0.1) local target = findNearestTorso(script.Parent.Torso.Position) if target ~= nil then if (script.Parent.Torso.Position - target.Position).magnitude > distanceStopped then script.Parent.Humanoid:MoveTo(target.Position, target) else script.Parent.Humanoid:MoveTo(script.Parent.Torso.Position) end end end
Edit 2: Sorry for the confusion!! I'll try to explain this again. There are two variables(labeled more clearly now) called distanceMin and distanceMax. distanceMin is the distance where the monster stops following and just sits there. distanceMax is basically the range of the monster(this feature was already built in but I just labeled it more clearly). If you don't want a max distance, then set the variable equal to math.huge(infinity). Hope this helps!
New script:
local larm = script.Parent:FindFirstChild("Left Arm") local rarm = script.Parent:FindFirstChild("Right Arm") distanceMin = 10 --Distance where the monster stops moving distanceMax = 100 --this is the distance your monster will see you (the range of the monster, so it doesn't start tracking you from across the map --if you don't want a max distance that it tracks(above) the set to infinity (distanceMax = math.huge) function findNearestTorso(pos) local list = game.Workspace:GetChildren() local torso = nil local temp = nil local dist = distanceMax local human = nil local temp2 = nil for x = 1, #list do temp2 = list[x] if (temp2.className == "Model") and (temp2 ~= script.Parent) then temp = temp2:findFirstChild("Torso") human = temp2:findFirstChild("Humanoid") if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then if (temp.Position - pos).magnitude < dist then torso = temp dist = (temp.Position - pos).magnitude end end end end return torso end while true do wait(0.1) local target = findNearestTorso(script.Parent.Torso.Position) if target ~= nil then if (script.Parent.Torso.Position - target.Position).magnitude > distanceMin then script.Parent.Humanoid:MoveTo(target.Position, target) else script.Parent.Humanoid:MoveTo(script.Parent.Torso.Position) end end end