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

CFrame.lookVector not working as expected?

The code is supposed to open a drawer inside a nightstand. When the Model is rotated at (0,0,0) the code works as intended. When I rotate the model to (0, 90, 0) the code rotates the Drawer back to (0, 0, 0)

I need the model to be able to be place anywhere on a map and be rotated at any angle and still function as intended. I plan on reusing this model across the map and do not want to have to change the position for every Drawer.

script.Parent.DrawerOpen.Value = false

script.Parent.ClickDetector.MouseClick:connect(function(OpenDrawer)
	if script.Parent.DrawerOpen.Value == false then
		script.Parent.CFrame = CFrame.new(script.Parent.Position + (script.Parent.CFrame.lookVector * -1))
		script.Parent.DrawerOpen.Value = true
		else if script.Parent.DrawerOpen.Value == true then
			script.Parent.CFrame = CFrame.new(script.Parent.Position + (script.Parent.CFrame.lookVector * 1))
			script.Parent.DrawerOpen.Value = false
		end
	end
end)

Thanks!

1 answer

✓ Accepted answer

The problem is that you're using position. Position doesn't include rotation. Therefore your part gets rotated back to 0, 0, 0.

CFrame includes rotation. Therefore adding to the part's CFrame will let rotation be maintained.

part.CFrame = part.CFrame * CFrame.new(1, 0, 0)

You may wait to edit the Z (instead of X) depending which way you made the 'front' of the drawer.

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