Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I CFrame a part to the character's head?

So I'm trying to make a gravity game where the camera is always attached to the players head, even if upside-down. I've tried this code just for the movement of the camera (will add angles later) but it does not attach to the head as required.

local campart = workspace.CameraPart
local player = game.Players.LocalPlayer
local torso = player.Character.Head
local offsetCFrame = Vector3.new(7, 0, 0)

latch.CFrame = torso.CFrame:ToWorldSpace(offsetCFrame)

The code needs to just 'weld' the camera to the player's head with an offset of about 7 studs. If anyone can help I would be very grateful. And if you don't understand something, comment about it and I will clarify.

3 comments

0

maybe change line 6 to latch.CFrame = torso.CFrame + offsetCFrame

0

no, that didn't work, the output mentions on line 3 'attempt to index nil with 'Character''

0

you need to wait for the character to be loaded, just add repeat wait() until game.Players.LocalPlayer.Character

1 answer

Constantly set the CFrame ok good thanks

local Camera = workspace.CurrentCamera
local p = game:GetService("Players").LocalPlayer


game:GetService("RunService").RenderStepped:Connect(function()
	if p.Character then
		local Attach = p.Character:FindFirstChild("Head")
		if Attach then
			Camera.CFrame = Attach.CFrame
		end
	end
end)

Give upvote

2 comments

0

ok yes but now how do i add the offset

0

nvm i did it

This discussion is preserved as it was. New answers, comments, and votes are closed.