That is my tween code currently and I found out if I put mouse.Hit.Position.Z + 1 at the end well it would kinda not do it. But now I cant figure out the math to actually make that “1” be calculated and not just a random number. Ive tried mouse.Hit.Position.Z / Slide.Position.Z but it didnt work.
Here's code that has the behavior you don't want (and some other unrelated issues)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local exitCurrentState
function enterState(fEnterState, ...)
if fExitCurrentState then
fExitCurrentState()
end
fExitCurrentState = fEnterState(...)
end
function idleState()
local c1; c1 = mouse.Button1Down:Connect(function()
local target = mouse.Target
if target and target.Parent.Name == "Slider" then
enterState(draggingState, target.Parent)
end
end)
return function()
c1:Disconnect()
end
end
function draggingState(slider)
local track, handle = slider.Track, slider.Handle
local function update()
local mouseSliderPos = track.CFrame:PointToObjectSpace(mouse.Hit.p)
local z = math.clamp(mouseSliderPos.Z, -track.Size.Z/2 + 1, track.Size.Z/2 - 1)
slider.Handle.CFrame = slider.Track.CFrame:ToWorldSpace(CFrame.new(0, 0, z))
end
local c1; c1 = mouse.Button1Up:Connect(function() enterState(idleState) end)
local c2; c2 = mouse.Move:Connect(update)
update()
return function()
c1:Disconnect()
c2:Disconnect()
end
end
enterState(idleState)
And here’s a fixed version that stores the offset from the mouse to the handle when the dragging begins, and makes sure to always keep the handle at that same offset. That way it never jumps around, it only follows the movement of the mouse:
Code
function draggingState(slider)
local track, handle = slider.Track, slider.Handle
local zOffset
do
local mouseSliderPos = track.CFrame:PointToObjectSpace(mouse.Hit.p)
local handleSliderPos = track.CFrame:PointToObjectSpace(handle.Position)
zOffset = handleSliderPos.Z - mouseSliderPos.Z
end
local function update()
local mouseSliderPos = track.CFrame:PointToObjectSpace(mouse.Hit.p)
local z = math.clamp(mouseSliderPos.Z + zOffset, -track.Size.Z/2 + 1, track.Size.Z/2 - 1)
slider.Handle.CFrame = slider.Track.CFrame:ToWorldSpace(CFrame.new(0, 0, z))
end
local c1; c1 = mouse.Button1Up:Connect(function() enterState(idleState) end)
local c2; c2 = mouse.Move:Connect(update)
update()
return function()
c1:Disconnect()
c2:Disconnect()
end
end