How to make a lever moved by mouse?

Hello. I was wondering how you would make a lever moved by mouse similar to how this video shared by @TopBagon shows it.


All help is appreciated. Thank you!

1 Like

Hi, judging from the video it appears that the level is being moved by the 3d position of the clients mouse. The moving of the level is most likely just the manipulation of a Motor6d Instance.

To Simulate this effect, create a Motor6d, set the Part0 to the switch panel; and then set the Part1 to the lever. We then want to move the lever when the mouse is touching it, this can be done by checking the Mouse.Target which returns the part the mouse is currently hovering over. Place a part around the switch area so we can ultilize this detection.

Now, this might be sort of confusing but we need to change the c0 property of the switch. This will basicly create the animation all the while being connected with the motor6d yay!

To tell if a part is a lever, we can use collectionservice and tag such. We dont want the switch to go 360 degrees… or wherever someone manages to get their mouse lol.

To Solve this, we use math.clamp() to “CLAMP” a position which basicly means we keep it between a constraint. Ex: cannot be lower than 1 and not higher than 10.

--Rough pseudo code (Local script) aka this will not work
local TweenService = game:GetService("TweenService") -- For smooth easing
local RunService = game:GetService("RunService") -- Interaction loop

local Motor6d = path.to.motor

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse() -- Get LocalPlayer's mouse

RunService.RenderStepped:Connect(function(delta)
    if(Mouse.Target == Lever) then -- Get lever somehow lol
        local mousePos = Mouse.Origin -- Get mouse 3d space pos

        Motor6d.C0 = Motor6d.C0:Lerp(CFrame.new(0, math.clamp(mousePos.X, math.rad(75), math.rad(-75), 0), delta)
    end
end)

Disclaimer: the code is not tested and is not designed to work.

Hopefully, rough code could serve as a reference when your really coding this.

I hoped this helped.

1 Like

Hi, I’ve edited this, but the Motor6D object does nothing. Even when manually changing the values being changed in the script, nothing happens.

The motor6d.C0 property is the “CFrame” property of the constraint. It can be used to create effects and link things together. I’ve thrown together an example in a place file so you can poke at it and see real world application. What I’ve done is a pretty accurate demo of what you would want, however I know its not perfect It should act as a reference at the very least.

The main localscript is located within StarterCharacter → StarterPlayerScripts.

Link to file:
LeverExample.rbxl (40.9 KB)

If you have any further questions, PM me and I’ll give you my discord tag.