Rotation Bug whenever "R" key is pressed

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
  • I want it so whenever i tap on the key “R” it rotates, it works but it doesn’t always does.
  1. What is the issue? Include screenshots / videos if possible!

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

  • Alot but none was by my topic :frowning:

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Here is the code i have.

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R and script.Parent.Visible == true then
		if axis2 == false and selected ~= nil then	
			axis2 = true

			if selected.Orientation == Vector3.new(0,0,0) then
				game.ReplicatedStorage.Events.Rotation:FireServer(selected, 90)
			elseif selected.Orientation == Vector3.new(0,90,0) then
				game.ReplicatedStorage.Events.Rotation:FireServer(selected, 180)
			elseif selected.Orientation == Vector3.new(0,180,0) then
				game.ReplicatedStorage.Events.Rotation:FireServer(selected, -90)
			elseif selected.Orientation == Vector3.new(0,-90,0) then
				game.ReplicatedStorage.Events.Rotation:FireServer(selected, 0)
			end	
		end
	end	
end)

So basically, you can only rotate it once per selection or what?? I am a bit confused by the issue here, but I may have a clue: axis2 is set to true but never to false in the code example, could that affect it?

I can only rotate it once when it is selected, after unselecting and selecting it i can rotate it again but only once, and axis2 is set to false when the mouse clicks something else but not the block.

Yyyeah that is quite the issue then, you are never setting it back after rotating and only after clicking something else.

Could using task.wait() to wait and then setting axis2 back to false work?

Yeah, that was the issue lol :sweat_smile:
Well, thank you so much for helping me!

Instead of the if statements why don’t you just do

game.ReplicatedStorage.Events.Rotation:FireServer(selected, (selected.Orientation + Vector3.new(0, 90, 0)).Y)
1 Like

Well, yeah that’s more simple but it sometimes bugs and goes at like 45 degrees.

I actually considered suggesting a simpler way but ended up not doing it, basically what I could suggest is:

-- // could put this at the start of the script
-- // ideally, in my opinion the services should be retrieved there
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Events = ReplicatedStorage:WaitForChild("Events")

------------------- ACTUAL FIX
local newCFrame = selected.CFrame * CFrame.Angles(0, math.pi / 2, 0)
-- // make pi negative to make it go in the opposite direction

local rotY = select(2, newCFrame:ToEulerAnglesXYZ())
Events.Rotation:FireServer(selected, math.deg(rotY))

Oh also, you should consider using an attribute to define the real CFrame (the CFrame it’s being tweened to) of the object! The reason why it went at 45 degree angles and such is because you tried to rotate it while it’s already being tweened, you could instead try using that attribute.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.