Changing CFrames using a Server script, help needed!

Hello,
Please let me know if my code is wrong and how I should fix it.
This is in Server Script Service:

player.CharacterAdded:Connect(function(character)
	character.Humanoid.Died:Connect(function()
		wait(2)
		player:LoadCharacter()
		print(Stage.Value)
		local H = character:FindFirstChild'HumanoidRootPart'
		if Stage.Value ~= 0 then
			H.CFrame = workspace.Obby.Checkpoints[Stage.Value].PrimaryPart.CFrame + CFrame.new(0,20,0)
			print(Stage.Value)
		end	
	end)
end)
--Note: It takes more than 15 seconds to respawn when I kill my character.
1 Like

I don’t see anything wrong with it right away, only that this

is just reinventing the wheel. The RespawnTime property exists under the Players service which you can change to 2. And is likely the issue here:

You can’t add CFrames; only multiply. So change the + to a *

Multiplying will do the same thing or it will increase it 20 times?

I don’t know exactly how it works but think of it like adding CFrames, more like adding their positions in this case at least. CFrame.new(0, 2, 0)*CFrame.new(0, 20, 0) for example results in CFrame.new(0, 22, 0) and not CFrame.new(0, 40, 0)

A very nice post on the topic:

1 Like

You should just add a Vector3.new(0,20,0) to the CFrame

Here is CFrame Math is you want to melt your brain.

So it won’t require wait(2)? And will it wait 2 seconds if I just change it in Players’ Service?

I did that but then someone told me CFrames can’t be added with Vector3

You can add a Vector3 to a CFrame but you can’t add a CFrame to a Vector3
(yes the order of the operation actually matters)

print(CFrame.new(0, 5, 0) + Vector3.new(0, 2, 0)) will work. Vector3’s are just a position. CFrames have both a position and a rotation. The result will be a CFrame value in the end equivalent to CFrame.new(0, 7, 0)

print(Vector3.new(0, 2, 0) + CFrame.new(0, 5, 0)) will error. CFrames also have rotational components which can not be added to a Vector3 since it has none of those.

1 Like

These are the math operations for CFrames

You can multiply CFrames with each other, order matters (different in different order)
and
Add, Subtract and multiply Vector3’s to/from CFrames

This gives me error.

player.CharacterAdded:Connect(function(character)
	character.Humanoid.Died:Connect(function()
		wait(2)
		player:LoadCharacter()
		print(Stage.Value)
		local H = character:FindFirstChild'HumanoidRootPart'
		if Stage.Value ~= 0 then
			H.CFrame = workspace.Obby.Checkpoints[Stage.Value].PrimaryPart.CFrame + CFrame.new(0,20,0)
			print(Stage.Value)
		end	
	end)
end)
--OUTPUT
  1
12:23:14.629 - ServerScriptService.Stats:51: attempt to index nil with 'CFrame'
12:23:14.635 - Stack Begin
12:23:14.636 - Script 'ServerScriptService.Stats', Line 51
12:23:14.637 - Stack End

This appears to return nil, then you try to get the cframe of nil, which is the error.

It actually is 1 as it printed before error. The model’s name is 1 too.

Stage.Value is 1
Which likely is the name of a model

but that sequence is for some reason returning nil
put

print(workspace.Obby.Checkpoints[Stage.Value].PrimaryPart)

right before it.

Edit: Maybe the Humanoid Root Part is nil. I’m gonna print(H)

THAT’S WHAT I THOUGHT - It’s nil…

player.CharacterAdded:Connect(function(character)
	character.Humanoid.Died:Connect(function()
		player:LoadCharacter()
		print(workspace.Obby.Checkpoints[Stage.Value].PrimaryPart)
		print(Stage.Value)
		local H = character:FindFirstChild'HumanoidRootPart'
		if Stage.Value ~= 0 then
			H.CFrame = workspace.Obby.Checkpoints[Stage.Value].PrimaryPart.CFrame * Vector3.new(0,20,0)
			print(Stage.Value)
		end	
	end)
end)

--Gives the output as MeshPart which is the name of primary part

How should I find HumanoidRootPart @Pharyx_Styx ?

I see, I completely missed your H.CFrame :stuck_out_tongue:

You set the CFrame of the character not the humanoid
Characters are Models
Use these to find and modify the CFrames of models.
GetPrimaryPartCFrame
SetPrimaryPartCFrame

Like this?

character:SetPrimaryPartCFrame(workspace.Obby.Checkpoints[Stage.Value].PrimaryPart.CFrame * Vector3.new(0,20,0))
--ERROR
Unable to cast Vector3 to CoordinateFrame
Oh I was multiplying it.

Yeah you’re supposed to add Vector3 to CFrame

I was actually changing cframe of its primary part, so it’s same thing right?