I’m trying to make a Slider System and apply it to buttons that are in a Rotated Frame.
The issue is, the frame is rotated, therefore it ends up working inproperly.
Below is the code which I structured into a module I can use for anything.
Code
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
export type main = {
new : (GuiButton) -> (NumberValue),
}
local function findDragParent(button)
local p1 = button.Parent
local p2 = p1.Parent
if p1 and p1:IsA("GuiObject") and p1.AbsoluteSize ~= button.AbsoluteSize then return p1 end
if p2 and p2:IsA("GuiObject") and p2.AbsoluteSize ~= button.AbsoluteSize then return p2 end
end
--[[unique features
*can find drag find based on its absoluteSize
*can add in frames with buttons inside of them
--]]
local DraggerService = {} :: main
function DraggerService.new(guiObject)
if guiObject and guiObject:IsA("GuiObject") then
local button
if guiObject:IsA("GuiButton") then
button = guiObject
else
button = guiObject:FindFirstChildWhichIsA("GuiButton")
end
if not button then return end
local dragParent = findDragParent(button)
local numberValue = Instance.new("NumberValue")
numberValue.Parent = button
local connection
local function destroyConnection()
if connection then
connection:Disconnect()
connection = nil
end
end
button.MouseButton1Down:Connect(function()
if connection then return end
connection = RunService.Heartbeat:Connect(function()
local mousePos = UserInputService:GetMouseLocation()
local relativePos = (mousePos - dragParent.AbsolutePosition)
--local percent = relativePos.Magnitude/dragParent.AbsoluteSize.Magnitude
local percent = relativePos.Magnitude/dragParent.AbsoluteSize.Magnitude
numberValue.Value = percent
local exitDistance = (mousePos - button.AbsolutePosition).Magnitude
if exitDistance >= 250 then
destroyConnection()
end
end)
end)
button.MouseButton1Up:Connect(destroyConnection)
button.Destroying:Once(destroyConnection)
return numberValue
end
end
return RunService:IsServer() and table.freeze(DraggerService) or DraggerService
The calculations are in the button.MouseButton1Down.
I’ve tried using trigonometry and using the rotation as theta but nothing has worked.