Error while moving an object

Hello Developers! I’m trying to make an electrical panel, and I’m having some problems, I will now answer these questions.

  1. What do you want to achieve? Keep it simple and clear!
    I want to move an object, then if I click again it moves to the other side again…
  2. What is the issue? Include screenshots / videos if possible!
    The issue is when I click it it moves to a position I don’t want,
    Not correct position when I click it the first time: image,
    Correct position when I click it the second time, but it should move to that position on the first click: image,
    then it repeats to get to this position:
    image,
    What I'm trying to say: It should move to this position on the first click: image
    On the second click:image
    What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I didn’t see any topic talking about this, if I’m wrong please link me to the topic, I tried searching in the Devforum.

I already have a script, but I’m having a trouble with it, it moves to a incorrect position to get the first click and second click position, I don’t know why, I don’t have any errors on the output. I’m trying to make an electrical panel, so to do it more realistic I want to do the switch move, I hope you can help me.

Kindest regards,
Waum_a.

local ClickDetector = script.Parent.ClickDetector
local parent = script.Parent
local debounce = false
ClickDetector.MouseClick:Connect(function()
	if debounce == false then
		debounce = true
		parent.Orientation = parent.Orientation + Vector3.new(60, 90, 0)
	elseif debounce == true then
		debounce = false
		parent.Orientation = parent.Orientation + Vector3.new(-90, 90, 0)
	end
end)

Should you be adding the Vector3 value to the .Orientation value of the part? You might want to manually set what position (Vector3) and Orientation it should be when clicked.

Also, debounce isn’t typically used in that context. Instead of signaling a state, debounce typically makes it so that a user cannot click something in rapid sucession, so they set it to true before a process is finished, then to false after (with false signalling that you can now activate whatever it is again). Perhaps you’d want to rename that variable to switch or something like that, just to clarify for others.

1 Like

Did you try switching the directions around?

1 Like

What do you mean by manually set the position?

Why not just set the position to it and not add to it?

1 Like

Huh? I know to move objects you must use Vector3, is any other way to do it?

Something like
parent.Orientation = Vector3.new(60, 90, 0)
elseif debounce == true then
debounce = false
parent.Orientation = Vector3.new(-90, 90, 0)

1 Like

Do you have to use elseif? isnt it easier to just use else?

local ClickDetector = script.Parent.ClickDetector
local parent = script.Parent
local debounce = false
ClickDetector.MouseClick:Connect(function()
	if debounce == false then
		debounce = true
		parent.Orientation = parent.Orientation + Vector3.new(60, 90, 0)
	else 
		debounce = false
		parent.Orientation = parent.Orientation + Vector3.new(-90, 90, 0)
	end
end)
1 Like