Player's HumanoidRootPart not getting set to part CFrame

I’ve a script that detects and changes a part’s name on PlayerAdded and I want to teleport the player to the part that has the same name as their but it doesn’t seem to set it.

No errors in the output, script is in ServerScriptService.

local Players = game:GetService("Players")

local playerHangerRegionsFolder = workspace["Player Hangar Regions"]

local DEFAULT_PART_NAME = "Part"

Players.PlayerAdded:Connect(function(player)	
	
	for _, hangar in pairs(playerHangerRegionsFolder:GetChildren()) do
		if hangar.Name == DEFAULT_PART_NAME then
			hangar.Name = player.Name 
			break
		end
	end 
	
	player.CharacterAdded:Connect(function(character)
        print("Fired") -- prints..
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
		humanoidRootPart.CFrame = playerHangerRegionsFolder[player.Name].CFrame + Vector3.new(0, 5, 0)
	end)
end)
1 Like

Try using WFC instead, since the HRP may not be available at run-time:

local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
1 Like

I printed out humanoidrootpart and it printed, so I don’t think that’s the problem but I’ll try it.

Edit: Doesn’t work either.

Also, instead of manually setting the CFrame use SetPrimaryPartCFrame

character:SetPrimaryPartCFrame(playerHangerRegionsFolder[player.Name].CFrame + Vector3.new(0, 5, 0))
2 Likes

Your math is also incorrect I think you meant to do this:

character:SetPrimaryPartCFrame(playerHangerRegionsFolder[player.Name].CFrame * CFrame.new(0, 5, 0))

You have to wait a little so character fully loads and your script should work correctly.

It isn’t incorrect, you can add CFrame’s with vectors.

I know but it does not do what you think…

Yes it does what I think. I set the CFrame and then add it by Vector3.new(0, 5, 0) on the Y axis.

The problem may be this. You can’t move an instance immediately after it is created. Try this

wait()
character:SetPrimaryPartCFrame(...

if you want to move it by vectors then char:MoveTo(Vector3.new(30,30,30))

it just waits the minimum amount of seconds

That’s not my point here, I was explaining that to cjjdwag.

Yes, that’s the way roblox works. Here’s the explanation in the documentation. You can also use RunService.Stepped:wait()

You have to wait for character to load inside of workspace. I am telling this a second time. Here is a script:

workspace:WaitForChild(player.Name,4)
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
humanoidRootPart.CFrame = playerHangerRegionsFolder[player.Name].CFrame + Vector3.new(0, 5, 0)

No its not the same thing, CFrames will do it in object space while the vector will do it in world space (CFrame | Documentation - Roblox Creator Hub), anyways this is what you need to do:

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local playerHangerRegionsFolder = workspace["Player Hangar Regions"]

local DEFAULT_PART_NAME = "Part"

Players.PlayerAdded:Connect(function(player)	
	
	for _, hangar in pairs(playerHangerRegionsFolder:GetChildren()) do
		if hangar.Name == DEFAULT_PART_NAME then
			hangar.Name = player.Name 
			break
		end
	end 
	
	local Character = player.Character or player.CharacterAdded:Wait()
		
	RunService.Stepped:Wait()
    Character:SetPrimaryPartCFrame(playerHangerRegionsFolder[player.Name].CFrame + Vector3.new(0, 5, 0))
end)
2 Likes

Yes I know, I explained it for a different purpose.

First, you know you can do :FindFirstChild(DEFAULT_PART_NAME) instead of looping and finding one yourself (and I know that under the hood it loops anyways, but it look a lot cleaner).

Second, if it’s not setting position the way you expect, maybe you aren’t finding a hangar that has the default name (Part)? It doesn’t make sense otherwise… Either the default name isn’t “Part” or your hangar folder is empty based on what I’m seeing. It could be something else but I’m not sure, I can’t think of anything else it could be. Try adding a print inside the loop to see what it’s doing.