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

How to make a humanoid look at its target using BodyGyro?

Asked by 10 years ago
torso = script.Parent.Torso
minrange = 20
target = nil
tor = nil
head = script.Parent.Head

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 <= minrange)  then
                        target = plr.Character
                    else
                        target = nil
                    end 
                end
            end
        end
    end
end

while true do
    wait(0.1)
    findNearestTarget()
    if target ~= nil then
        if ((torso.Position - tor.Position).magnitude <= minrange) then
            script.Parent.Torso.BodyGyro = CFrame.Angles()--Piece of the cords needed!
        else
            target = nil
        end
    else
        target =nil
    end 
end

This is the script that will find nearest target. If the target is less then 20 studs away of the target, he's supposed to start looking at it. I don't understand the body gyro, i already puted alot f values between the paranthesis and the mob just starts spazing out. Please help me? Note: This is a flying creature.

Answer this question