Slider gui isn't working properly

Hi, I’m trying to create a slider GUI, which moves with mouse’s X position, but it’s not moving correctly, here’s the code:

local SliderBTN = script.Parent

local IsButtonHeld = false

local Mouse = game.Players.LocalPlayer:GetMouse()


SliderBTN.MouseButton1Down:Connect(function()
	IsButtonHeld = true
end)


SliderBTN.MouseButton1Up:Connect(function()
	IsButtonHeld = false
end)


Mouse.Move:Connect(function()
	if IsButtonHeld then
		SliderBTN.Position = UDim2.new(0, Mouse.X, 0,0)
	end
end)

Help is appreciated! :slight_smile:

Could you link a video or gif of the problem?

I’m guessing that you want your slider to be relative to a frame’s boundaries therefore this probably won’t work because it’s relative to the player’s screen.

I don’t know how I can send a video or gif, I’m new

Is there any solution for that?

robloxapp-20200813-0556588.wmv (200.7 KB)

Yeah you can use something like math.clamp to solve this.
maybe math.clamp(Mouse.X - z, x, y)

z would be the slider frames absolute position X,
x would be the left x position of the frame,
y would be the right x position of the frame

You mean math.clamp(Mouse.X - SliderBTN.Parent.AbsolutePosition.X, 0, 1)

1 Like

yeah but instead of the 1 it would be like the length of the slider gui frame

what do you mean by length? I don’t understand

Something like this, would give you the appropriate X position based off the frame it’s in

Let me try, hope this works. I’ll tryu

Also, should I change the position on Offset or scale?

1 Like

It’s preference, but offset would be easier math to deal with

Scale 100%. Scale makes it able to be used on other devices.

On the other hand, @Crowdsource is correct about offset.