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.
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
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
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
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.
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.
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)