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.
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)
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.
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
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
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
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.