Help with math.clamp()

When the player drags the bar, the value that is returned from the module script ranges from -0.5 to 0.5.
The problem is, only a small section of the bar seems to be actually changing value, with a section of the lower and upper half of the bar returning 1.5.

The max and min values should only be returned when the player drags the bar completely full or completely empty, I think(?) I’m doing something wrong with my math.clamp in the local script, but I’m not sure.

https://gyazo.com/e5cf69a7d4169df97b604714dcee8dd4
image

Local Script

local function dragSlider(_, _, input)
	local BarSlider = SliderManager.new(HeightBar:FindFirstChildWhichIsA("Frame"))	-- Construct slider class
	local size = BarSlider:DragBar(input.Position) -- passing in the mouse location
	HeightValue = math.clamp(HeightValue+size, 0.5, 1.5)	-- Clamp the player height to between 0.5 and 1.5
	print(HeightValue)
end

Module Script

function SliderManager:DragBar()	-- Drags slider and returns slider value(s)
	-- Dragging bar visual effect
	local absolutePos = Vector2.new(self.BarButton.AbsolutePosition.X, self.BarButton.AbsolutePosition.Y)
	local mouseLoc = UserInputService:GetMouseLocation()
	local max = self.BarButton.AbsoluteSize.X
	self.Bar.Size = UDim2.new(0, math.clamp(mouseLoc.X - absolutePos.X, 0, max), 1, 0) -- bar size changes position to mouse x position
	
	-- Calculate bar value out of 100
	local MaxSize = Vector2.new(self.BarButton.AbsoluteSize.X, self.BarButton.AbsoluteSize.Y)
	local size = self.Bar.AbsoluteSize
	-- "+0.5" , any value above 0.5 rounds up
	-- 100 is the total range of values, while -50 is the min value
	local value = math.floor((100 * (size.X / MaxSize.X) + 0.5)-50)

	return value/100
end

You shouldn’t clamp the result of DragBar at all, since the slider code already clamps it.

However the slider returns a value in the range [-0.5, 0.5]. I don’t know why it doesn’t just do 0 to 1 but hey your code :slight_smile:

Also, I don’t know what HeightValue is but I don’t think you mean to add the size value to it every frame. Why not just use/print size Instead?

Are you trying to remap size (which again will be between -0.5 and 0.5) to the range [0.5, 1.5]? Because then it would just be

local remapped = size + 1
print(remapped)

I think I should be more clearer.
HeightValue is the default height of my custom character (1), therefore, when adding the value returned by my module script, the variable would be added to (1 + 0.5) or subtracted by (1 + (-0.5)).

I’ve tried not clamping the sum of HeightValue + size previously, this was the results:
https://gyazo.com/44a031714aa78e91f2dc5ace34990746

Interestingly enough, instead of the bar, the position of my mouse is dictating the result of HeightValue + size, although I don’t know why.

I think this line is the issue.

You’re modifying HeightValue every time your mouse moves, based on what it was the last time.

So even if you wiggled your mouse in place that size stayed at 0.1 example, HeightValue would increase every time: 0, 0.1, 0.2, 0.3, 0.4, etc. forever.

Instead just set it directly.

Instead of HeightValue = HeightValue + size just do HeightValue = 1 + size

1 Like