Identical character added and character there already functions producing different results

local function PlayerAdded(player)
	if player.Character then
		print("character already there")
		local position = CFrame.new(-24.741, 8.897, 34.543)
		local orientation = CFrame.Angles(0,math.rad(90),0)
		player.Character:WaitForChild("HumanoidRootPart")
		player.Character:SetPrimaryPartCFrame(position*orientation)
		player.Character:PivotTo(position*orientation)
		print(player.Character:GetPivot())
		DisablePlayerCollision(player.Character)
		player.Character.DescendantAdded:Connect(DisablePlayerCollision)
	end
	player.CharacterAdded:Connect(function(character)
		print("character added")
		local position = CFrame.new(-24.741, 8.897, 34.543)
		local orientation = CFrame.Angles(0,math.rad(90),0)
		player.Character:WaitForChild("HumanoidRootPart")
		player.Character:SetPrimaryPartCFrame(position*orientation)
		player.Character:PivotTo(position*orientation)
		print(player.Character:GetPivot())
		DisablePlayerCollision(player.Character)
		player.Character.DescendantAdded:Connect(DisablePlayerCollision)
	end)
	

As you can, see, the code segments do the exact same thing, but, when the character added function runs, it consistently puts the player in the wrong orientation, while the “character there” one does it correctly. How can I fix this?

Wait for the character to be parented to workspace.

player.CharacterAdded:Connect(function(character)
	character.AncestryChanged:Wait()
	...

Also, why are you using player.Character if the function already has a character argument.

I was trying to see if maybe that was what caused the problem (it wasn’t). Also, I’m still facing the same issue trying what you suggested.

Apparently, waiting for the character to be parented is not enough.

Do this again but this time add a task.wait() after waiting for the character to parent.

player.CharacterAdded:Connect(function(character)
	character.AncestryChanged:Wait()
	task.wait() -- You can add a wait value if you want

Unless there are other stuff in your code that changes the cframe on spawn, this should work.

2 Likes
character.AncestryChanged:Wait()

This could permenantly yield the function’s execution (in the event that when ‘CharacterAdded’ is fired the character has already been parented to the ‘Workspace’ container). While an annoying fix the task.wait() should suffice.

I have thought about this but everytime I do this, I have never seen this infinitely yeilding. Thus, I believe it is extremely rare to happen.

Assuming Player:LoadCharacter() has the same events with CharacterAutoLoads, Player.CharacterAdded fires before parenting, well parenting to DataModel? (not sure why it parents to DataModel)

But it is still better to be safe than sorry.

Right, why didn’t I think about that. Instantaneous code can finish in just a heartbeat.