Volume Ui Slider

How to fix? whats the issue i dont know whats wrong (i dont understand math scripting stuff so its probably something with the math) it doesnt slide it just jumps from side to side and it only prints the number 10

function MoveVolume(screenPosition)
	local yDiff = screenPosition.x - slider.AbsolutePosition.x
	local knobPositionRatio = math.min(math.max(yDiff / slider.AbsoluteSize.x, 0), 1)

	local newFOV = 10 + 0 * (1 - knobPositionRatio)
	knob.Position = UDim2.new(knobPositionRatio, 0, 0, 0)
	print(newFOV)
end
knob.InputBegan:connect(function(input)
	if (input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch) then
		knobGrabbed = true
	end
end)

UIS.InputChanged:connect(function(input, gameProcessedEvent)
	if (not gameProcessedEvent and (input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch) and knobGrabbed) then
		MoveVolume(input.Position)
	end
end)

UIS.InputEnded:connect(function(input, gameProcessedEvent)
	if (not gameProcessedEvent and input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch) then
		knobGrabbed = false
	end
end)

1 Like

Instead of UIS.InputChanged maybe it’ll work by binding a loop in the InputBegan and unBinding it in InputEnded?

knob.InputBegan:connect(function(input)
	if (input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch) then
		knobGrabbed = true
                game:GetService("RunService").BindToRenderStep("moveUI", 301, MoveVolume(input.Position))
	end
end)

UIS.InputEnded:connect(function(input, gameProcessedEvent)
	if (not gameProcessedEvent and input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch) then
		knobGrabbed = false
                game:GetService("RunService"):UnbindFromRenderStep("moveUI")
	end
end)

Although please check the syntax in studio to see if its all correct (I forgot syntax, this is how I remember)

2 Likes

The variable newFOV will print 10 everytime because you are multiplying the (1 - knobPositionRatio) by 0 every single time. Is this what you are trying to achieve? In case you are unfamiliar with BIDMAS, which is the order of operations; Brackets first, then Indices (to the power of and stuff like that), Division, then Multiplication, then Addition, then Subtraction. Since multiplication comes first, you are multiplying 0 by the ratio every single time (because any number multiplied by 0 will always be 0, since something times nothing is always nothing) and adding 10 to it which means your output will be 10 every single time.

1 Like

have you tired using the new ui drag dector i think there is a way to make the ui only move on the X axis im pretty sure byteblox made a video about them and then calulate a way to measure how far along it is on the frame or what it is on

1 Like

Just to add on to what you said, and to clarify a misconception that a lot of people have in regards to PEMDAS/BODMAS/BIDMAS etc. (depending on which country you were taught in).

The actual mathematical version is PE(MD)(AS) / BO(DM)(AS) / BI(DM)(AS), where multiplication AND division have the same priority, and addition AND subtraction have the same priority. In lua (and maths in general), when you have two same-priority operators, you go from left-to-right (left-associative).

This means that despite PEMDAS (American version of BIDMAS) suggesting that division comes after multiplication, in this equation:

8/2*(2+2)

The answer is 16 as you do 2+2=4 first, then 8/2=4, then 4*4=16.

Doing it in the “order” of PEMDAS would equate to:
2+2=4, 2*4=8, 8/8=1

You can learn more about Lua’s operator precedence here
More information about PEMDAS misconceptions

Basic rule of thumb is that when you have an equation with more than one * or /, or more than one + or -, solve from left-to-right.

The more commonly used acronym now being taught is ‘GEMS’ (as described in the article I shared)

1 Like

ive found a solution thanks everyone