How would I restore the position of this lever?

Hello, I am using a new roblox studio feature that is still in beta called ‘DragDetectors’ to make my lever functional on the server. They do lag a little but they are really nice, but there is one problem that I just cannot fix, once clicking on the lever and trying to set its value, it always starts at its original position.
Here is the video explaining it.

I want this to be able to retain its position and allow the player to drag the lever up and down from where they left off instead of having to start from its original.

Here is the code:

function Electronics.Lever(Lever,LogisticPort)
	
	local DragDetector = Lever.DragDetectorPart.DragDetector
	local main = Lever.Main
	local Motor6D = main.Motor6D

	local offset = CFrame.new(-0.01,-0.677,-0.3)

	DragDetector:GetPropertyChangedSignal('DragFrame'):Connect(function()

		local DragY = math.clamp(DragDetector.DragFrame.Position.Y,-main.Size.Y + 0.7,0)

		local powered = math.floor(DragY / (-main.Size.Y + 0.7) * 100) / 100

		Motor6D.C1 = offset * CFrame.new(0,-DragY,0)
		
		setPowered(Lever,powered)
	end)
end

Note: DragDetector.DragFrame always returns to 0 once you start dragging, i’ve tried to find a setting to change this default but couldn’t find anything.

Any help is appreciated.

2 Likes

Try using Motor6D.C1.Position (initialPosition) to maybe prevent the reset of the position like this:

function Electronics.Lever(Lever, LogisticPort)
	local DragDetector = Lever.DragDetectorPart.DragDetector
	local main = workspace.main
	local Motor6D = main.Motor6D

	local offset = CFrame.new(-0.01, -0.677, -0.3)

	-- Store the initial lever position
	local initialPosition = Motor6D.C1.Position

	DragDetector:GetPropertyChangedSignal('DragFrame'):Connect(function()
		local DragY = math.clamp(DragDetector.DragFrame.Position.Y, -main.Size.Y + 0.7, 0)
		local powered = math.floor(DragY / (-main.Size.Y + 0.7) * 100) / 100

		-- Update the lever's position based on the drag
		Motor6D.C1 = CFrame.new(initialPosition + offset.p + Vector3.new(0, -DragY, 0))

		setPowered(Lever, powered)
	end)
end


Nope, still doesn’t work. Thanks for the reply though, I remember that I tried doing something similar.

Maybe try using RunService.Heartbeat:

local RunService = game:GetService("RunService")

-- rest of your previous code
DragDetector:GetPropertyChangedSignal('DragFrame'):Connect(function()
	local DragY = math.clamp(DragDetector.DragFrame.Position.Y, -main.Size.Y + 0.7, 0)
	local powered = math.floor(DragY / (-main.Size.Y + 0.7) * 100) / 100

	RunService.Heartbeat:Connect(function() -- Use RenderStepped if LocalScript
		Motor6D.C1 = CFrame.new(initialPosition + offset.p + Vector3.new(0, -DragY, 0))
	end)

	setPowered(Lever, powered)
end)
end

Nope that just crashed my game haha

oh then add a wait(.01) i forgor lol

Nope

I gave up on this stuff and just started sending the original cframe to a cframe value stored before my object moves. Then to restore, I aimed the movement back on the stored cframe.Value. Turns out this works at any angle. so … There is a cframe that works just like a bool. You can store a cframe and get that data back that way. Even if the object moved to a new position and then you wanted to move it back.

Something like this in pseudo code …

local db = false
function()
 db = not db

 if db then 
   store cframe value to a cframe.Value
   move lever down
 else 
   get stored cframe.value
   move lever back to that value
 end
end

Try:

function Electronics.Lever(Lever, LogisticPort)
    local DragDetector = Lever.DragDetectorPart.DragDetector
    local main = Lever.Main
    local Motor6D = main.Motor6D

    local offset = CFrame.new(-0.01, -0.677, -0.3)
    local initialDragY = 0  -- Initialize the initial drag position to 0

    DragDetector:GetPropertyChangedSignal('DragFrame'):Connect(function()
        local currentDragY = DragDetector.DragFrame.Position.Y
        local deltaDragY = currentDragY - initialDragY  -- Calculate the change in drag position
        local clampedDragY = math.clamp(deltaDragY, -main.Size.Y + 0.7, 0)
        local powered = math.floor(clampedDragY / (-main.Size.Y + 0.7) * 100) / 100
        Motor6D.C1 = offset * CFrame.new(0, -clampedDragY, 0)

        setPowered(Lever, powered)

        -- Update the initial drag position for the next frame
        initialDragY = currentDragY
    end)
end


Nope, sorry.

Why you are changing position of the lever instead chaning the current position?

Sorry I don’t understand, I am changing the motor6d c1 of the lever instead of the position and cframe because I need this lever to be welded onto moving vehicles later on, and the only way that I know of for moving/tweened parts to work on unanchored, moving parts is by welding via motor6d.

Try again, please. I updated it.

Well, what i am seeing in your code that you are setting new cframe of c1 every time is dragged

Motor6D.C1 = offset * CFrame.new(0,-DragY,0) what are you doing here is just setting offset and the Y axis and never get the current Y axis of the c1

you just need something to get the current cframe and add the current cframe to the new cframe and you need to check if it’s not going out of the lever zone/offset.

example like how drag ui works.

1 Like


Nope, but thanks for all the replies!

I already get the cframe and convert it into a vector3 here, and I did try yoshicoolTV’s script and Swetch29’s with me adding the inital position, I also did something similar before but it didn’t work.

local DragY = math.clamp(DragDetector.DragFrame.Position.Y,-main.Size.Y + 0.7,0)

Can you upload your lever model then, please?

If you do it’ll be easier to solve your problem by making it easier to debug the solutions we give you.

Sure!
levermodel.rbxm (12.3 KB)
Does this work?

A lot easier when you give what’s needed to fix the problem

local Lever = script.Parent
local DragDetector = Lever.DragDetectorPart.DragDetector
local main = Lever.Main
local Motor6D = main.Motor6D
local offset = CFrame.new(-0.01,-0.677,-0.3)

DragDetector:GetPropertyChangedSignal('DragFrame'):Connect(function()
	local DragY = math.clamp(DragDetector.DragFrame.Position.Y, -main.Size.Y + 0.7, 0)
	local powered = math.floor(DragY / (-main.Size.Y + 0.7) * 100) / 100
	Motor6D.C1 = offset * CFrame.new(0,-DragY,0)
end)


Nope

edit: I guess I should’ve included the model in the original post in the first place, my bad.

Works perfectly for me … you have something else wrong.
Try that in a new game. And how did you post that video?