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

Why isn't my CFrame code working? I want the PlaneModel.engine to point towards the mouse.hit

I have a model plane anchored in the sky and I'm trying to make it point to mouse.hit, here's my script

plane = game.Workspace.PlaneModel.Engine

mouseMove = script.Parent.Parent.Parent:GetMouse()

function planeRot()
	game.Workspace:waitForChild("PlaneModel")
	mousePos = mouseMove.Hit.p
	plane.CFrame = CFrame.new(plane.CFrame, mousePos)
end
mouseMove.Move:connect(planeRot)

The script is inside of tool, hence the parents Output: 20:09:29.637 - Players.Player1.Backpack.Plane.Script:35: bad argument #1 to 'new' (Vector3 expected, got CFrame)

1 answer

✓ Accepted answer

Firstly, when you use the 2-argument CFrame constructor, both arguments need to be of type Vector3, not of type CFrame. On line 8, you're passing plane.CFrame as the first argument, but this should be either plane.Position or plane.CFrame.p. See this section about CFrame constructors on the Official ROBLOX Wiki for more detail.

Secondly, you only need to WaitForChild on PlaneModel once, not every time that the function planeRot() gets triggered. On line 1, you're trying to reference the engine of the plane, but if the plane hasn't loaded yet then this could produce an error. You should wait for the PlaneModel before trying to access its engine.

Thirdly, since you want to make the whole model face in a certain direction, it would be better to use SetPrimaryPartCFrame on the model rather than manually setting the CFrame of the engine. Make sure that you set the PrimaryPart of the model to be the engine, too.

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