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

How can I stop a humanoid from getting too close to a player?

Asked by 8 years ago
Edited 8 years ago

I have been working on a new project and wanted to incorporate pets or followers, However when I made them follow the players the pet kept walking into the player pushing the player. Is there a way I can make it so that they only follow the player if there is a big enough gap between the two? here is the script that I have been using

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 = 30
    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 = findNearestTorso(script.Parent.Torso.Position)
    if target ~= nil then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end
end
0
Idk really how but I was thinking maybe you can have parts around the "pet" so the pet could not get too close.. FiredDusk 1466 — 8y
0
But then the parts around the pet would push you PikminLegion 38 — 8y
0
Just make the pet move to a certain distance away from the player than a body part of the player alphawolvess 1784 — 8y

Answer this question