Trying to rotate a part when right clicking

Im trying to make a part turn to the part2 when i press right click again
script:

``

local textbutton = script.Parent

local function rightClick()
print(“hi”)

local camera = game.Workspace.CurrentCamera

workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable

workspace.CurrentCamera.CFrame = game.Workspace.Part.CFrame 

if workspace.CurrentCamera.CFrame == game.Workspace.Part.CFrame then

workspace.CurrentCamera.CFrame = game.Workspace.Part2.CFrame

end	

end

textbutton.MouseButton2Click:Connect(rightClick)

I’m not sure what you mean by right-click again because the script looks fine to me.

1 Like

for e.g if i press e to open a gui and then press e again to close it but this time its if you press left click to go onto part 1 and you press left click again to go onto part 2

local textbutton = script.Parent
local Debounce = false
local camera = game.Workspace.CurrentCamera

local function rightClick()
	if Debounce == false then
		workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
		workspace.CurrentCamera.CFrame = game.Workspace.Part.CFrame 
		Debounce = true
	else
		workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
		workspace.CurrentCamera.CFrame = game.Workspace.Part2.CFrame
		Debounce = false
	end
end

textbutton.MouseButton2Click:Connect(rightClick)

I added a debounce and it will now work perfectly!

1 Like

when i right clicked once it went onto part 1 but when i pressed again it never went onto part 2

Thanks for giving me a heads up!

1 Like

also why did it needed a debounce?

Debounces give you 2 options in 1 function, If false then it can do 1 operation and 2 for another. In your case you want the same button to have 2 different outcomes depending on how many times you clicked it.

By clicking it once it switches the debounce on and does operation 1 and then when you click it again it will do the other operation and switchback down to the first one.

1 Like

A debounce is a cooldown which prevents a function from executing too frequently, for example.

local Debounce = false

local function Welcome()
	if Debounce then --end function if debounce is true
		return
	end
	
	Debounce = true --if we made it here enable the debounce
	print("Hello world!") --do code
	task.wait(2) --wait for some time (2 seconds in this case)
	Debounce = false --disable the debounce
end

Welcome() --print command in function is executed and debounce is enabled
Welcome() --debounce is enabled so the function ends before the print command
task.wait(3) --wait for the debounce to expire
Welcome() --performs the same as it did when it was first called

What you’re referring to is a switch/toggle.

1 Like
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local textbutton = script.Parent
local camera = workspace.CurrentCamera
local part1 = workspace:WaitForChild("Part1")
local part2 = workspace:WaitForChild("Part2")

local toggle = false

local function leftClick() --left click button to reset camera
	if camera.CameraType ~= Enum.CameraType.Custom then --only set camera type when necessary
		camera.CFrame = CFrame.new(0, 0, 0)
		camera.CameraSubject = humanoid --return focus of the camera back to the player's character
		camera.CameraType = Enum.CameraType.Custom
	end
end

local function rightClick()
	if camera.CameraType ~= Enum.CameraType.Scriptable then --only set camera type when necessary
		camera.CameraType = Enum.CameraType.Scriptable
	end

	toggle = not toggle --flip toggle (switch)
	camera.CFrame = toggle and part1.CFrame or part2.CFrame --focus the camera on either part depending on the toggle's state
end

textbutton.MouseButton1Click:Connect(leftClick)
textbutton.MouseButton2Click:Connect(rightClick)

If you need any assistance with anything feel free to reach out. I have tested this & it works.

1 Like