How to get 0-100 slider position

Hey, i need help getting a value from a slider i scripted, here:

script.Parent.MouseButton1Down:Connect(function()
	Enabled = true
	local OriginPosX = script.Parent.Position.X.Scale
	while UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) and Enabled do
		task.wait()
		local X = Mouse.X-script.Parent.Parent.AbsolutePosition.X
		script.Parent.Position = UDim2.new(0,math.clamp(X,script.Parent.Parent.AbsolutePosition.X-script.Parent.Parent.AbsoluteSize.X/2,script.Parent.Parent.AbsolutePosition.X+script.Parent.Parent.AbsoluteSize.X/2),-1.154,0)
		local Amount = script.Parent.Position.X.Offset/5
		game.Lighting.ClockTime = Amount
	end
end)

How would i go about getting a value from 0-100 from this slider? i’m kinda stuck, any help is appreciated.

1 Like

Have the slider’s maximum size represent 100%.

Since you’re working with 0-24 values by using ClockTime, you are going to want to use linear interpolation to find a value between 0 and 24, not a value between 0 and 100.

Essentially, you would first want to find a value between 0 and 1: the position between the min and max of the slider.

This is relatively self-explanatory since you’re probably going to be using the AbsolutePosition and AbsoluteSize properties.

-- minX would be 0 because it would just be absolutePosition.X - absolutePosition.X
local maxX = script.Parent.Parent.AbsoluteSize.X -- this will be the equivalent of 1, once again we don't need to do any complex math because to find it relative to 0, it would just be absolutePosition.X + absoluteSize.X - absolutePosition.X

-- now to find the value between 0 and 1, we need to find the position relative to the min and max bounds:
local mouseXRelativeToObject = mouse.X - script.Parent.Parent.AbsolutePosition.X -- this should be correct, if not, try flipping sides (can't remember brain fog lol). it's the mouse's position relative to the left side

-- now that we have these two values, we can find the value between 0 and 1 (the percentage/alpha)
local alpha = math.clamp(mouse.X / maxX, 0, 1) -- just in case it's below 0 or above 1, in which case we would need to cut it off

-- now, to answer your question to find a value between 0 and 100, you would just multiply alpha by 100
local percentage = alpha * 100 --> should be a number between 0 and 100

-- now, since you're looking for a value that starts at 0 and ends somewhere above 0, it's pretty easy to figure out,
local time = 24 * percentage

-- this is because since you're looking for a value that is between 0 and 24 (inferring because you are setting the ClockTime property of lighting which takes a value between 0 and 24), we need to use the linear interpolation formula, which is just:
min + (max - min) * alpha

-- so, we need to do the following to find our value "proportionally" scaled up between 0 and 24:
local value = 0 + (24 - 0) * alpha

-- since it's just adding and subtracting, we can remove the add/subtract operations that involve 0, which is essentially
local value = 24 * alpha

This second part is a bit off-topic but it’s relevant if you’re looking to do more slider math:

-- the lerping formula is really useful when you want to find a value that does not start with 0, so let's say you want to find a value that is 43% between 53.22 and 142.65
-- obviously, that would be a bit difficult to do in your head, so we can plug these values into our linear interpolation formula:
local value = 53.22 + (142.65 - 53.22) * 0.43 -- 43% is equal to 0.43 jsyk
-- now, value is equal to 91.6749, which is the value that is 43% between 53.22 and 142.65

never mind i just took the sliderXOffset/sliderMaxOffset
then i woould get a value to 0-1 and then i just multiplied it by 24

1 Like