Help Make Slider bar

hello, I’m making a bar slider and i need help making the bar move to the mouse position when the Hitbox is clicked, i don’t have the script but i need help to make it where the X size of the bar tweens the mouse’s X Position(I think its the x Position)

i tried this, but dident work


local LowestValue = 0
local MaxValue = 10
local IsAtMax = false

local HitBox = script.Parent
local Bar = script.Parent.Parent.MainBar
local CurrentNumber = script.Parent.Parent.CurrentNumber
local CurrentNumberValue = script.Parent.Parent.CurrentNumberValue

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



HitBox.MouseButton1Click:Connect(function()
	local Pos = Mouse.X - Bar.AbsolutePosition.X
	if Pos < 0 or Pos == 0 then
		print("Is At 0")
		Pos = 0
	end
	if Pos > Bar.AbsoluteSize.X or Pos == Bar.AbsoluteSize.X then
		print("Reached The Max")
		Pos = Bar.AbsoluteSize.X
	end
	if not IsAtMax then
		print("Is Moving")
		CurrentNumberValue.Value = math.floor((Pos/Bar.AbsoluteSize.X)*MaxValue+0.5)
	else
		CurrentNumberValue.Value = (Pos/Bar.AbsoluteSize.X)*MaxValue
	end
end)

You have a very good start. Try it like this:

--< Services
local UserInputService = game:GetService("UserInputService")

--< Variable
local Container = script.Parent

local ContainerAbsoluteSize = Container.AbsoluteSize
local ContainerAbsolutePosition = Container.AbsolutePosition

local Bar = Container:WaitForChild("Bar")

local Button = Container:WaitForChild("Button")

local InputHolded = false

--< Functions
local function UpdateSlider()
	--// Variables
	local mouseLocation = UserInputService:GetMouseLocation()
	local alpha = (mouseLocation.X - ContainerAbsolutePosition.X) / ContainerAbsoluteSize.X
	
	--//
	Bar.Size = UDim2.new(math.clamp(alpha, 0, 1), 0, 1, 0)
end

--< Connections
Button.MouseButton1Down:Connect(function()
	UpdateSlider()
	InputHolded = true
end)

UserInputService.InputEnded:Connect(function(inputObject)
	if (inputObject.UserInputType == Enum.UserInputType.MouseButton1) and InputHolded then
		InputHolded = false
	end
end)

UserInputService.InputChanged:Connect(function(inputObject)
	if (inputObject.UserInputType == Enum.UserInputType.MouseMovement) and InputHolded then
		UpdateSlider()
	end
end)

Only change the values to match yours.