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

Character won't look at mouse pointer?

Why won't this code work? The character should look at the mouse pointer, but I get the error "CFrame is not a valid member of Model". This is in a local script in starterplayer.

while true do
	local player = game.Players.LocalPlayer
	local mouse = player:GetMouse()
	local char = player.Character
	if not char or not char.Parent then
    char = player.CharacterAdded:wait()
end
	char.CFrame = CFrame.new(char:WaitForChild("HumanoidRootPart").Position, mouse.Hit.p)
	wait()
end

1 answer

Right, so there are a couple issues going on here.

First, the error.

CFrame is not a valid member of Model

Models are like containers, logical folders that don't have a physical presence in the 3D world. For this reason, they don't have physical coordinates and therefore do not have a CFrame.

--

How can you get around this?

The most consistent way to set a CFrame for an entire model, is to use the Model's SetPrimaryPartCFrame method. How this works, is you identify a single part in the model you want it to move off of, so often a center part or a part you'd want it to rotate on if you are pivoting.

In R15, the character's PrimaryPart by default is the HumanoidRootPart, but in R6 it is the Head.

--

So what about code?

char.PrimaryPart = char:FindFirstChild("HumanoidRootPart") --in case of R6

char:SetPrimaryPartCFrame(char.PrimaryPart.Position, mouse.Hit.p)

--

Next issue, is that you aren't checking for the case where there is no mouse.Hit(i.e. you point your cursor into the void where there are no parts and it returns nil).

To avoid another error, you would want to make the code only run if mouse.Hit exists.

local hrp = char:FindFirstChild("HumanoidRootPart")
char.PrimaryPart = hrp --in case of R6

if mouse.Hit then
	char:SetPrimaryPartCFrame(hrp.Position, mouse.Hit.p)
end

--

Last, you may find that even with these changes successfully implemented, you will find your character glitching in a weird way and that you actually only wanted the character to rotate only on one axis so that you turn the direction the mouse is, but your body doesn't topple over itself constantly.

The axis that people most commonly want to rotate on is the Y axis, which will allow your character to look side to side but not up and down.

To accomplish only rotating on one axis, we are going to substitute in the current height of the HumanoidRootPart where the y component of the mouse.Hit.p would be.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local char = player.Character

while true do
	if not char or not char.Parent then
		char = player.CharacterAdded:Wait()
	end
	local hrp = char:WaitForChild("HumanoidRootPart")
	char.PrimaryPart = hrp
	if mouse.Hit then
		local lookAt = Vector3.new(mouse.Hit.p.X, hrp.Position.Y, mouse.Hit.p.Z)
		char:SetPrimaryPartCFrame(CFrame.new(hrp.Position, lookAt))
		wait()
	end
end

This probably looks close to what you want, but still pretty glitchy. The last major improvement we can implement is instead of using a while loop and a wait(), we can use a function connected by the RenderStepped event of RunService.

Final code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local char = player.Character

local rs = game:GetService("RunService").RenderStepped

rs:Connect(function()
	if not char or not char.Parent then
		char = player.CharacterAdded:Wait()
	end
	local hrp = char:WaitForChild("HumanoidRootPart")
	char.PrimaryPart = hrp
	if mouse.Hit then
		local lookAt = Vector3.new(mouse.Hit.p.X, hrp.Position.Y, mouse.Hit.p.Z)
		char:SetPrimaryPartCFrame(CFrame.new(hrp.Position, lookAt))
	end
end)

--

Let me know if you have any questions.

-amanda

2 comments

1

Thank you for all of that work! Now let's just hope it works! :)

0

I tested it all out in Studio in a step by step process, the final block of code should be fully functional, it is just a matter of if it is what you are trying to accomplish.

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