Ad
Log in to vote
0

Does Anyone Know How I Can Get This Follow Script To Find a Specific Player?

Asked by 6 months ago

So Basically what im trying to do is get this script t only follow 1 player around at all times. Does anyone know how I can accomplish this? I dont want it to follow every player on the map I just want it to follow whatever player I assign it too. Here is the script:(Thanks in Advance For Any Help)

bin = script.Parent

function move(target)
    local dir = (target.Position - bin.Position).unit
    local spawnPos = bin.Position
    local pos = spawnPos + (dir * 1)
    bin:findFirstChild("BodyGyro").cframe = CFrame.new(pos,  pos + dir)
    bin:findFirstChild("BodyGyro").maxTorque = Vector3.new(9000,9000,9000)
end

function moveTo(target)
    bin.BodyPosition.position = target.Position
    bin.BodyPosition.maxForce = Vector3.new(10000,10000,10000) * bin.Speed.Value
end

function findNearestTorso(pos)
    local list = game.Workspace:GetChildren()
    local torso = nil
    local dist = 1000
    local dist2 = 10
    local temp = nil
    local human = nil
    local temp2 = nil
wait(2)

    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("HumanoidRootPart")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist and (temp.Position - pos).magnitude > dist2 then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
            end
        end

    end
    return torso
end

function shoot(pos)
    dir = (pos - bin.CFrame.p).unit 
    for i = 1, 50 do 
        local ex = Instance.new("Explosion") 
        ex.BlastRadius = 1
        ex.Position = bin.Position + (dir * 10 * i) + (dir * 7) 
        ex.Parent = game.Workspace 
    end
end

function shootAt(torso)
    local dir = (torso.Position - bin.Position).unit
    local spawnPos = bin.Position
    local pos = spawnPos + (dir * 1)
    shoot(pos)
end

while true do
    local torso = findNearestTorso(bin.Position)
    if torso~=nil then
        move(torso)
        moveTo(torso)
    end
    wait()
end

0
A lot of deprecated code not made by you. incapaz 6384 — 6mo

1 answer

Log in to vote
0
Answered by 6 months ago
Edited 6 months ago

Edit: My previous code was riddled with errors, but this one works.

local bin = script.Parent
local target = nil --Target to be chased
local run = true --Variable to cancel while loop whenever
wait(5)

local function filter(temp,min,max)
    local hum = temp:findFirstChild("Humanoid")
    local root = temp:findFirstChild("HumanoidRootPart")

    if hum == nil or root == nil then
        return false
    end
    local dist = (root.Position-bin.Position).magnitude
    print(dist)
    print(hum.Health)
    return dist < max and dist > min and hum.Health > 0
end
local function findTarget(pos)
    local list = game.Workspace:GetChildren()
    local maxDist = 1000
    local minDist = 10
    local tar = nil

    for i=0,#list do
        local temp = list[i]
        if temp == nil then

        elseif temp.ClassName == "Model" and temp ~= bin then
            if filter(temp,minDist,maxDist) then
                tar = temp
                maxDist = (tar.Torso.Position-bin.Position).magnitude
            end
        end
    end
    return tar
end
local function moveTo(tar)
    print("Moving")--Replace
end

while run do
    if target ~= nil then
        if target:findFirstChild("Humanoid") == nil or target:findFirstChild("Torso") == nil then   --Can't remember whether Lua's syntax uses !boolean
            target = findTarget(bin.Position)
        else
            moveTo(target)
        end
    else
        target = findTarget(bin.Position)
    end
    wait(5)
end

Not trying to rip on you or anything of the sort, but it seems like you didn't even read the code or don't or understand it. You could have solved it yourself if you'd just moved the torso variable outside the while loop and only fired the findNearest Torso function when the current was dead or null(nil). All I did above was the former and made it less cluttered. (Haven't tested it yet, but it should work) Edit: Changed temp in while loop to target and moved "filter" function above "findTarget" function. Java has spoiled me :D

0
To be honest it was late and I had just got done about 11 hours straight of coding a game. My mind was fried. Thanks for the help though CrazyRoblots 13 — 6mo
0
No prob. Meltdown81 304 — 6mo
Ad

Answer this question