Slider still moves after player releases mouse

So basically I have this slider and it functions perfectly however there is one issue. The first time I try to use it everything goes accordingly however it’s meant to stop sliding whenever the mousebutton1up is fired however when i go off the slider and go back onto it there is an issue. The slider keeps sliding despite me not clicking on it.

local BarDown = false

MusicProgress.MouseButton1Down:Connect(function()
	BarDown = true
end)

MusicProgress.MouseButton1Up:Connect(function()
	BarDown = false
end)

MusicSlider.MouseMoved:Connect(function()
	if BarDown then
		local Value = math.floor((((tonumber(365) - tonumber(minvalue)) / 365) * MusicSlider.AbsoluteSize.X) + tonumber(minvalue))

		if (math.clamp(Mouse.X - MusicProgress.AbsolutePosition.X, 0, 365)) < 50 then
			MusicSlider.Size = UDim2.new(0, Value, 0, 24)
		end	

		MusicProgress.Size = UDim2.new(0, math.clamp(Mouse.X - MusicProgress.AbsolutePosition.X, 0, 365), 0, 24)

		MusicValue.Text = tostring( (math.clamp(Mouse.X - MusicProgress.AbsolutePosition.X, 0, 365)/100) )..' / 3.65'
	end
end)

try using game:GetService('UserInputService') your problem more likely the mouse isnt on the button anymore when you release it, so the MouseButton1Up wont fire

local uis = game:GetService('UserInputService')
uis.InputEnded:Connect(function(input,g)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		BarDown = false
	end
end)
4 Likes

How would I optimise this so it only fires when i’m over the bar?

Also does this work on mobile devices?

you can utilize the BarDown. if its true then the function will run and vice versa

local uis = game:GetService('UserInputService')
uis.InputEnded:Connect(function(input,g)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and BarDown then
		BarDown = false
	end
end)

absolutely, i remember having the same issues and no one can solve it XD

It does work but it still hovers when I go back onto it

hmmm, something must be wrong with my solution. are you deleting the

MusicProgress.MouseButton1Down:Connect(function()
	BarDown = true
end)

?

1 Like

Yeah I did. I realised it after writing the reply so I edited it. But it’s still not working it still changes size when I hover over it.

you mean, its still moving when you released it?

well yeah ig it changes size though so yeah it still changes size after i release it and move while on it

There is absolutely no reason on why you should be using tonumber on something that is already a number.

plus, Lua will still do the math correctly if the number is a string.

I forgot to get rid of that thanks. I didn’t know the exact math for the slider so I just got the code from pastebin and it had that for some reason.

If the Slider is a Frame then you can connect directly to it’s own InputBegan/InputEnded.
Here’s an example of a Slider that uses it when being dragged by MouseButton1 or Touch:

local UiS=game:GetService("UserInputService")
local Fr=script.Parent:WaitForChild("Frame")
local Slider=Fr:WaitForChild("Slider")
local isActive=false
local iMouse=Enum.UserInputType.MouseButton1
local iTouch=Enum.UserInputType.Touch

local function GetXPos(frame)

	local minXPos=frame.AbsolutePosition.X
	local maxXPos=minXPos+frame.AbsoluteSize.X
	local xPixelSize=maxXPos-minXPos
	local mouseX=(UiS:GetMouseLocation().X)
	mouseX=math.clamp(mouseX, minXPos, maxXPos)
	return (mouseX-minXPos)/xPixelSize

end

local function DragSlider()
	
	repeat task.wait()
		local xPos=GetXPos(Fr)
		Slider.Position=UDim2.fromScale(xPos, .5)
	until not isActive
	
end
Slider.InputBegan:Connect(function(i) if i.UserInputType==iMouse or i.UserInputType==iTouch then isActive=true DragSlider() end end)
Slider.InputEnded:Connect(function(i) if i.UserInputType==iMouse or i.UserInputType==iTouch then isActive=false end  end)

You can literally see mousebutton1down and mousebutton1up in my code why would it be a frame?

GuiButtons also have their own InputBegan/InputEnded, I only mentioned Frame as it’s used in the example I posted - maybe I should’ve made that more clear.

There’s a problem with this though I already have it done for when they click or touch it. I need it to run while the players mouse is held over the progress bar so it can continously change size to wherever they move their mouse. Input began only fires once.