Slider not working properly

Hello Devs

I am pretty new to scripting so I cant make things like sliders and stuff, so I followed a tutorial on youtube that explained how to make this slider, but it has one problem.

Clicking anywhere on the slider makes it change value, like it should. But if I hold down my mouse button the value changes to a greater one for some reason. For example, if I hold down my cursor on 50, the value will be set to 75. The gif below shows what happens

roblox-issue

this is the layout inside the explorer
image

this is the “Update” script

local mouse = game.Players.LocalPlayer:GetMouse()
local slider = script.Parent
local fill = script.Parent.Fill
local trigger = script.Parent.Trigger
local outputValue = script.Parent.OutputValue
local outputLabel = script.Parent.Label

local maxValue = 1 or 100/100
local startingValue = 0.5 or 50/100

fill.Size = UDim2.fromScale(outputValue.Value,1)
outputLabel.Text = tostring(math.round(outputValue.Value*100))

local tweenService = game:GetService("TweenService")
local tweenStyle = TweenInfo.new(0.25,Enum.EasingStyle.Exponential)

function UpdateSlider()
	local output = math.clamp(((Vector2.new(mouse.X,mouse.Y)-slider.AbsolutePosition)/slider.AbsoluteSize).X,0,1)
	
	local outputClamped = startingValue + (output*(maxValue-startingValue))
	
	if outputValue.Value ~= outputClamped then
		tweenService:Create(fill,tweenStyle,{Size = UDim2.fromScale(output,1)}):Play()
	end
	
	outputValue.Value = outputClamped
	outputLabel.Text = tostring(math.round(outputValue.Value*100))
	print(output)
end

local sliderActive = false

fill:GetPropertyChangedSignal("Size"):Connect(function()
	outputLabel.Text = tostring(math.round(fill.Size.X.Scale*100))
end)

function ActivateSlider()
	sliderActive = true
	while sliderActive do 
		UpdateSlider()
		task.wait()
	end
end

workspace.MusicRandom:GetDescendants()

trigger.MouseButton1Down:Connect(ActivateSlider)

game:GetService("UserInputService").InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		sliderActive = false
	end
end)

If anyone knows why that happens, please tell me why
Thanks

Idk what happened but why didn’t you use UI Drag Detectors?

This might be happening because the UpdateSlider function is continuously called in the while sliderActive loop.

You only make it work once and enable the sliderActive then inside a mouse.Move event check if sliderActive is true then call the function again:

local mouse = game.Players.LocalPlayer:GetMouse()
local slider = script.Parent
local fill = script.Parent.Fill
local trigger = script.Parent.Trigger
local outputValue = script.Parent.OutputValue
local outputLabel = script.Parent.Label

local maxValue = 1 or 100/100
local startingValue = 0.5 or 50/100

fill.Size = UDim2.fromScale(outputValue.Value,1)
outputLabel.Text = tostring(math.round(outputValue.Value*100))

local tweenService = game:GetService("TweenService")
local tweenStyle = TweenInfo.new(0.25,Enum.EasingStyle.Exponential)

local function UpdateSlider()
	local output = math.clamp(((Vector2.new(mouse.X,mouse.Y)-slider.AbsolutePosition)/slider.AbsoluteSize).X,0,1)
	local outputClamped = startingValue + (output*(maxValue-startingValue))
	
	if outputValue.Value ~= outputClamped then
		tweenService:Create(fill,tweenStyle,{Size = UDim2.fromScale(output,1)}):Play()
		outputValue.Value = outputClamped
		outputLabel.Text = tostring(math.round(outputValue.Value*100))
		print(output)
	end
end

local sliderActive = false

fill:GetPropertyChangedSignal("Size"):Connect(function()
	outputLabel.Text = tostring(math.round(fill.Size.X.Scale*100))
end)

local function ActivateSlider()
	sliderActive = true
	UpdateSlider()
end

trigger.MouseButton1Down:Connect(ActivateSlider)

game:GetService("UserInputService").InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		sliderActive = false
	end
end)

mouse.Move:Connect(function()
	if sliderActive then
		UpdateSlider()
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.