Setting a character facing position in CharacterAdded event

I am trying to get my character to face in a specific direction and the script I am using is called from the CharacterAdded event.

After reviewing several posts on this forum and a suggestion I found on YouTube, I could not get it to work for me

Below is my code showing the different code I tried and failed with. Any suggestion will be gratefully received

function PlayerHelper:SetFacingDirection(player: Player)
	local character = player.Character
	
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	local direction = game.Workspace.Lobby.Pirate.WorldPivot.Position 
	
	humanoidRootPart.Parent.Humanoid.AutoRotate = false
	
	--TRY 1
	--humanoidRootPart.CFrame = CFrame.lookAt(humanoidRootPart.Position, direction)
	
	--TRY 2
	--humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.CFrame.p, humanoidRootPart.CFrame.p + direction)
	
	--TRY 3
	--humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.CFrame.p, direction)
	
	--TRY 4
	--character:SetPrimaryPartCFrame(CFrame.new(humanoidRootPart.Position, direction.Unit))
	
	--TRY 5
	--character.HumanoidRootPart.CFrame *= CFrame.Angles(0, 0, math.rad(180))--JUST try some rotation
	
	--TRY 6
	--local newCFrame = CFrame.lookAt(humanoidRootPart.Position, direction)
	--humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position, direction)
	
	--TRY 7
	--local humanoid = character:WaitForChild("Humanoid")
	--local newOrientation = CFrame.new(Vector3.new(0, 0, 0), Vector3.new(0, 0, -1))
	--character:WaitForChild("HumanoidRootPart").CFrame = newOrientation
end

EDIT
Looking at ChatGpt it says the

it’s likely because character orientation may be overridden by the character controller

this seems reasonable but is there a way to resolve if this is the case?

1 Like

Maybe use both lookAt and CFrame?

function PlayerHelper:SetFacingDirection(player: Player)
    local character = player.Character
    if character then
        local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
        local direction = game.Workspace.Lobby.Pirate.WorldPivot.Position
        humanoidRootPart.Parent.Humanoid.AutoRotate = false

        local newCFrame = CFrame.lookAt(humanoidRootPart.Position, direction)
        humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position, direction)
    end
end

thanks for the suggestion but it has no effect :frowning:

I’m thinking that the chararter position must be an event that is triggered after the PlayerAdded event do you know what the last event that is trigger is?

Okay I have found the solution.
Thanks to those that tried to help, its really appreciated.

My problem seems to be that an event that faces the players direction gets triggered after the CharaterAdded event.

I was able to add a local script to StarterCharacterScripts. I can add a local script in here and it will trigger every time the player respawns and after the CharaterAdded event. A local script is valid in this case as it’s something to make a players life easier and make the game feels nicer.

local character = game.Players.LocalPlayer.Character

local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
local direction = Vector3.new(51.277935, 289.530121, 3218.95288)

--uncomment the below line if you want the charater to be locked to the direction you set
--humanoidRootPart.Parent.Humanoid.AutoRotate = false

humanoidRootPart.CFrame = CFrame.lookAt(humanoidRootPart.Position, direction)

Its worth noting that the docs says this about StarterCharacterScripts

The StarterCharacterScripts class stores scripts to be parented in a player’s Player.Character, when they spawn. Unlike scripts stored in the PlayerScripts folder, these scripts will not persist when the player respawns.

I took that to mean that after they were run once they were destroyed. That is not the case. every time a character is added they are run again…maybe it means it is run once for each time a character is added