Rotation not functioning

Hey! I just made a goalkeeper script, but I can’t rotate it manually.

local NPC = script.Parent
local ball = workspace:WaitForChild("Ball")
local region = workspace:WaitForChild("Region")

while true do
	local ballPos = ball.Position
	local regionPos = region.Position

	if ballPos.Z < regionPos.Z - region.Size.Z/2 then
		NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, regionPos.Z - region.Size.Z/2)
	elseif ballPos.Z > regionPos.Z + region.Size.Z/2 then
		NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, regionPos.Z + region.Size.Z/2)
	else
		NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, ballPos.Z)
	end
	wait(0.05)
end

What I mean is, I might want to change the rotation some time, but it is stuck at (0, 0, 0).

I tried adding this to the script, but then it didn’t work anymore:

NPC.HumanoidRootPart.CFrame.Orientation = Vector3.new(0, -90, 0)

It did rotate how I wanted it to, yet it stopped moving when I did that. Which I obviously also don’t want.

1 Like

Orientation is not a valid property of CFrame. Add CFrame.Angles(0, math.rad(-90), 0) after each CFrame operation instead.

CFrame.Angles only accepts radian values, so make sure you use math.rad(number) when editing rotations.

I meant CFrame.Rotation. Not Orientation.

Rotation cannot be assigned to.

Why can it not be assigned to? I said in the main text that it did rotate, only the script didn’t work.

Are you sure?

See the output if you have errors. Copy and paste the errors here if any.

No errors, also why did you use += instead of just =?

Just try using CFrame.Angles instead, as I have said above, then report back.

Also, using a += b is the same as a = a + b.

This above is what I used for rotation, even if += works, you did it different than mine, try that then tell me what happened, if it doesnt work, i’ll try yours

Already did:

A CFrame has positional and rotational data in it. CFrame.new() only makes a CFrame with positional data, and no rotational data (rotation at 0, 0, 0).

CFrame.Angles creates a CFrame with rotation, but no position.

If you set a part’s CFrame made by CFrame.new, you set the part’s position to whatever you like, but it will not have any rotation. This is literally the reason why it appears to rotate only once.

CFrame docs link:

I tried it in the command line, it didn’t work, yet when I try it in a script. It does rotate. I’m here about why the rest of the script doesn’t work.

What’s your modified script look like?

local NPC = script.Parent
local ball = workspace:WaitForChild("Ball")
local region = workspace:WaitForChild("Region")

while true do
	NPC.HumanoidRootPart.CFrame.Rotation = Vector3.new(0, -90, 0)
	local ballPos = ball.Position
	local regionPos = region.Position

	if ballPos.Z < regionPos.Z - region.Size.Z/2 then
		NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, regionPos.Z - region.Size.Z/2)
	elseif ballPos.Z > regionPos.Z + region.Size.Z/2 then
		NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, regionPos.Z + region.Size.Z/2)
	else
		NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, ballPos.Z)
	end
	wait(0.05)
end

Modified script ^

It does say rotation cannot be assigned to, yet it does rotate.

This is literally what you’re seeing. Observer how the Part’s rotation changes when I only use CFrame.new alone, and with CFrame.Angles.

Your character’s rotation actually resets to (0, 0, 0), not (0, 90, 0).


Not there. Set the rotations when you also set your model’s position:

if ballPos.Z < regionPos.Z - region.Size.Z/2 then
	NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, regionPos.Z - region.Size.Z/2) * CFrame.Angles(0, math.rad(YOUR ANGLE), 0)
elseif ballPos.Z > regionPos.Z + region.Size.Z/2 then
	NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, regionPos.Z + region.Size.Z/2) * CFrame.Angles(0, math.rad(YOUR ANGLE), 0)
else
	NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, ballPos.Z)
end

Yeah, now it doesn’t move. That’s what I’ve been trying to do.

Also when I enter -90, it doesn’t turn and when it does it’s only 45 degrees.

Do you have any errors? What else did you add to your script?

No errors, I added what you just sent to my script and changes the angle values

Do you have a video showing this problem?

1 Like

robloxapp-20230108-1608593.wmv (3.6 MB)