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

How can I make this follow script follow a part and a player depending on which is closer?

Asked by 6 years ago
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
    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 game.Players:GetPlayerFromCharacter(temp2) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
end
                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

-- This script makes a noob follow a player. How can I make it go to "game.Workspace.Gate" if it is closer to the gate and follow a player if its closer to a player?

I have been trying for an hour and have not gotten anything to work. I tried to do a

do blah()

else if blah magnitude > blag then

do blah()

--Thanks

0
After 2 years, no-one answered this.. Also, this is a free model script. niroqeo 123 — 4y
0
You don't have to reinvent the wheel birdeater11 14 — 2y

Answer this question