Rotation script not working

So I have a placement system where you can rotate a part, but the rotation does not work. I do this all on a local script, then replicate it to the server once the object is placed. But, the rotation does not work! There are absolutely zero errors.

Reference is the cloned model

local Rotation = UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.Key.Q then
		Reference:SetPrimaryPartCFrame(Reference:GetPrimaryPartCFrame() * CFrame.fromEulerAnglesYXZ(0, -1, 0))
	end
	if Input.KeyCode == Enum.KeyCode.E then
		Reference:SetPrimaryPartCFrame(Reference:GetPrimaryPartCFrame() * CFrame.fromEulerAnglesYXZ(0, 1, 0))
	end
end)

All help is appreciated

I think your issue might be here: if Input.KeyCode == Enum.KeyD then, you should be comparing input.KeyCode with an valid enum under Enum.KeyCode so changing Enum.KeyD to Enum.KeyCode.D should fix the problem( or part of it)

But i notice some other things in your code, Firstly You are disconnecting Rotation every time and an input has ended and when you disconnect UIS.InputBegan, it essentially sets rotation to nil, thus not allowing it to be Disconnect again. I personally would avoid using :Disconnect() in this case and instead use some boolen logic

1 Like

That was a typo, sorry, but, I tried NOT using disconnect, and it didn’t work doesn’t work.

1 Like

Ok, so this is odd then, i tested this (the code you provided) and with a model i placed in the workspace (as “Reference”) and everything worked as expected. So that leaves me with a question, Are inputs registering in your InputBegan function at all?

1 Like

Yes it is, I also use renderstepped from a previous line that sets the cframe of the model:

local Render = RS.RenderStepped:Connect(function()
	if Mouse.Target == workspace.Terrain then
		Reference:SetPrimaryPartCFrame(CFrame.new(Mouse.Hit.p.X, Mouse.Hit.p.Y, Mouse.Hit.p.Z))
	end
end)

does that affect it?

It would, because you’re resetting the rotation to default

1 Like

How would I counter that(I recently figured that out from changing render stepped to Mouse.Move, but that caused a series of problems)

May I suggest making a variable that you’d change for the rotation with your script, then in the DenderStepped, you apply the rotation:

local rot = 0

-- In the InputBegan function
if Input.KeyCode == Enum.KeyCode.Q then
   rot = -1
else if Input.KeyCode == Enum.KeyCode.E then
   rot = 1
end

-- in the RenderStepped
Reference:SetPrimaryPartCFrame(mouse.Hit * CFrame.fromEulerAnglesYXZ(0, rot * (math.pi/2), 0)

Cframe. FromEulerAngles takes radians, so if you have degrees and want ro turn that to radians, use: math.rad

1 Like

Our problem, is as @Raretendoblox( similar), has said you are not applying any rotation in your Render stepped loop. To fix this you could simply multiply this CFrame.new(Mouse.Hit.p.X, Mouse.Hit.p.Y, Mouse.Hit.p.Z) by the orientation/rotation you have already defined. Now there are a couple of recommendations i would like to give, I would recommend not Always using RenderStepped(just as a reminder) for anything that isn’t handling something “heavy” like camera manipulation, however it is ok ( in most cases recommended) to use render stepped for placement systems, just make sure you use it when you should and correctly. Mouse.Move might fix your problem, it can be unreliable though which is why you might want to instead wrap your Move Model Logic in a Runservice function or something that isn’t waiting on an input

1 Like

Yes, thank you, but how do I detect the q button being held down? I tried using while Input.KeyCode == KeyCode.Q then but it goes on forever

You also need to use InputEnded and define variables for each key. When the key is held down, the corresponding variable will be true, when it is releases it will go back to false

1 Like

I don’t know what you mean, can you explain a bit? Do you mean that I make a variable true if the key is pressed? And vice versa?

Yes. Say we have variables keyQ and keyE, in the InputBegan function and when the Q key is pressed (held down), keyQ will be true. InputEnded fires when a key is released, in that event you’d have to check if the input returned is Enum.KeyCode.Q. If it is, it means the Q key was released, thus we make keyQ false. The same thing applies for KeyE

2 Likes

As a SideNote: There are a couple of ways to do this, using the Userinputservice or not, but one way is to do is to do as Rartendoblox said, but the Another option using the UserInputService, is using IsKeyDown(enum.KeyCode)

1 Like

How would I add that to the script?

I tried using if key.KeyCode == IsKeyDown(Enum.KeyCode.Q) but it errors, how would I fix that?

thank you for the help in advance, I have to go

The reason this errors is because when you are using if key.KeyCode , you should be comparing it with a keycode, IsKeyDown is not a KeyCode, it is a function of the UserinputService and it returns whether the input specified is being pressed down. if you were to use this for checking if a key is pressed down, you would need to put it in a loop

local UIS = game:GetService("UserInputService")

while true do

wait(.1)

  if UIS:IsKeyDown(Enum.KeyCode.Q) then
   print("Q is being Pressed")  --- Q is being held down
  end

end