Hello!
I’ve recently made a slider class for my settings menu on my game, and everything was working up until this point.
So, I position the button based off of the mouse’s X position which is clamped. But, I now have the issue of positioning it when the player firsts joins, based off of their saved settings.
My normal mouse positioning code (for reference, works fine)
local mouseConnection = uis.InputChanged:Connect(function(input: InputObject)
if input.UserInputType ~= Enum.UserInputType.MouseMovement then return nil end --ignore if not mouse movement
--get the X value of the mouse
local pos = uis:GetMouseLocation()
local xPos = pos.X - self.__Elements.BarFull.AbsolutePosition.X
--clamp the X position to the bar bounds
xPos = math.clamp(xPos, 0, self.__Elements.BarFull.AbsoluteSize.X)
--calculate an alpha for how far along the button is out of the bar
local alpha = xPos / self.__Elements.BarFull.AbsoluteSize.X
--position the button across the bar
self.__Elements.Drag.Position = UDim2.fromOffset(
xPos - 15,
self.__Elements.BarFull.Position.Y.Offset + 3
)
--now, interpolate between the two values using the alpha
local newValue = math.floor(
lerp(
self.ValueBounds.min,
self.ValueBounds.max,
alpha
)
)
--store in the current value
self.__CurrentValue = newValue
--display the value on the current display
self.__Elements.Current.Text = newValue
--move the sliding bar with the slider
self.__Elements.SliderBar.Size = UDim2.new(0, xPos - 0.2, 1, 0)
end)
BarFull
is just the full bar, SliderBar
sizes with the slider to give a smooth effect, and Drag
is the button which is dragged to move it.
This is my problematic code:
--sorry for lack of comments this was a quick draft equation and it was the closest i got
--initialise the slider position with the given alpha
function Slider:_Init(alpha: number)
--ignore this line, it's so I get autocomplete from my type. I will remove it.
self = self :: Slider
local factor = self.__Elements.BarFull.Size.X.Scale / 2
local min, max = self.__Elements.BarFull.Position.X.Scale - factor, self.__Elements.BarFull.Position.X.Scale + factor
print(min, max)
local xPos = lerp(min, max, alpha)
self.__Elements.Drag.Position = UDim2.new(xPos, 0, 0, self.__Elements.Drag.Position.Y.Offset + 3)
end
--another script:
fovSlider:_Init(0)
musicSlider:_Init(0.25)
This is what it’s doing right now:
This is how it should look:
If you need any more info, just ask. Otherwise, I appreciate any help.