Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a monster stop at a certain distance?

Asked by 9 years ago

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

1 answer

Log in to vote
0
Answered by 9 years ago
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

0
I tried it and it didn't want to work so I read through it all. It seems like you made it go to the torsos' position if it was further than the given distance away and to move to the torso if it was closer than that too. Though thanks for the help :) Peanutthedestoyer 0 — 9y
0
There were two different variables the function and I was in a bit of a rush( Sorry :) ) So I didn't pay that much attention to the other distance variable "distance". I will edit my answer as soon as I fix this. tkddude2 75 — 9y
0
Alright the new script it up. tkddude2 75 — 9y
Ad

Answer this question