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

How can you get a player to look at a part?

Asked by 5 years ago

I've tried two ways one just CFraming the rootpart and the other using bodygyro but both don't seem to work.

Note: TroopA[3] is an table objectvalue of the players character

CFrame:

local startPosition = TroopA[3].HumanoidRootPart.Position
local targetPosition = Target.Hitbox.Position
TroopA[3].HumanoidRootPart.CFrame = CFrame.new(startPosition, targetPosition)

BodyGyro:

TroopA[3].HumanoidRootPart.BodyGyro.CFrame = CFrame.new(TroopA[3].HumanoidRootPart.Position, Target.Hitbox.Position)

2 answers

Log in to vote
0
Answered by 5 years ago

You should be able to do this with by tweening the player's humanoid root part to look at the part. It can be done seen here:

local lookAt = script.Parent -- Part.
local tweenService = game:GetService("TweenService")
local basicTweenInfo = TweenInfo.new(0.01) -- Tween time.

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        wait(5) -- To make sure the character is on the ground before doing this.

        local root = character:WaitForChild("HumanoidRootPart")

        spawn(function()
            while true do
                wait()
                tween(root)
            end
        end)
    end)
end)

function tween(part)
    local tweenGoals = {
        CFrame = CFrame.new(part.CFrame.Position, lookAt.CFrame.Position)
    }
    local createTween = tweenService:Create(part, basicTweenInfo, tweenGoals)
    createTween:Play()
end

May be a little shaky but this is only a written example.

Ad
Log in to vote
0
Answered by 5 years ago

Nvm I have stupid, if anyone else wants to know what I did just use this

local Object = Vector3.new( Target.Hitbox.Position.x, Target.Hitbox.Position.y, Target.Hitbox.Position.z )

TroopA[3].HumanoidRootPart.CFrame = CFrame.new( TroopA[3].HumanoidRootPart.Position, Object )

Answer this question