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

Making the humanoid constantly focused on the target?

Asked by 10 years ago

My follow script

torso = script.Parent.Torso
maxrange = 70
minrange = 20
target = nil
tor = nil
head = script.Parent.Head

torso.BodyPosition.position = Vector3.new(0,head.Position.Y+10,0)

function findNearestTarget()
    plrs = game.Players:GetChildren()
    for i, plr in ipairs(plrs) do
        if plr.Character ~= nil then
            if plr.Character:findFirstChild("Humanoid") and plr.Character.Humanoid.Health>0 then
                if plr.Character:findFirstChild("Torso") then
                    tor = plr.Character.Torso
                end 
                if target == nil then
                    if ((torso.Position - tor.Position).magnitude <= maxrange)  then
                        if ((torso.Position - tor.Position).magnitude >= minrange) then
                            target = plr.Character
                        else
                            target = nil
                            script.Parent.Zombie:MoveTo(script.Parent.Head.Position, script.Parent.Head)
                        end     
                    else
                        target = nil
                        script.Parent.Zombie:MoveTo(script.Parent.Head.Position, script.Parent.Head)
                    end
                end
            end
        end
    end
end

while true do 
    wait(.1)
    findNearestTarget()
    if target ~= nil then
        if ((torso.Position - tor.Position).magnitude <= maxrange) and ((torso.Position - tor.Position).magnitude >= minrange) then
            script.Parent.Zombie:MoveTo(target.Torso.Position, target.Torso)
            script.Parent.Torso.BodyPosition.position = Vector3.new(0,tor.Position.Y,0)
        else
            target = nil
        end
    else
        target =nil
    end     
end

So, what i want is that the humanoid keeps constantly focused at its target, aka, looking at the target. I really don't understand to much humanoids, so can someone tell me how to make the humanoid constantly looking at the target?

Answer this question