Issue initially positioning slider

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.

Where is xPos (I’m assuming that’s the players saved position) being retrieved from? It looks like you’re not seeing the saved value.
Try printing the saved value when the code runs the first time.
It may be that you need to add a WaitForChild when accessing the DataStores.
Also make sure you’re accessing the correct DataStores location.

i haven’t implemented the data store logic yet - I want to get the maths working correctly first. xPos is based off the alpha value, which differs between the sliders - for the music slider, it’s 0.25, so the slider should go 1/4 way along the bar, but it doesn’t. The FOV slider should sit at the left side of its bar, since the alpha is 0, but it doesn’t.

xPos is calculated based off of the alpha and the bottom and top bar positions - it’s an issue with calculating where min and max actually sit.

So the min max of the FOV is 70 and 110, a difference of 40.
0% is 0, 100% is 40, added to 70 is your actual FOV value.
Set your slider at 0% of 40, the digits on the screen don’t matter.

The digits on screen (maximum and minimum values) aren’t relevant. In my drag code, I first calculate the position based on the X value and then use that to interpolate for the actual value represented by the slider.

I found a “solution” which makes it almost work as expected. Since I set the bar size to the same size as the parent frame, I just interpolated between 0 and 1 and put this into the Scale part of the drag button’s UDim2 position.

There’s now a tiny little issue where it’s slightly offset. Here’s my new code:

function Slider:_Init(alpha: number)
    --still here so i can get autocomplete on my types
	self = self :: Slider
	
	--lerp the position between 0 and 1 because the bar is the same size as its parent
	local xPos = lerp(0, 1, alpha)
	
	--position the slider appropriately based off of the scale position
	self.__Elements.Drag.Position = UDim2.new(xPos, 0, 0, self.__Elements.Drag.Position.Y.Offset + 3)
end

I fixed it with a bit of both UDim2 elements and a slight offset. Thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.