Modifying slider to tween

I have this slider what i use to set values and i want to make it tween the size instead to look smoother how can i do this

here is the script

mouse = false
script.Parent.MouseButton1Down:connect(function() mouse=true  end)
script.Parent.MouseButton1Up:connect(function()   mouse=false end)
script.Parent.MouseLeave:connect(function(x,y) mouse=false end)
script.Parent.MouseMoved:connect(function(x,y)
	if mouse then
		if 10-(((y-script.Parent.AbsolutePosition.Y)/850)*1) > 2 then
			script.Parent.Value.Value = 15-(((y-script.Parent.AbsolutePosition.Y)/850)*15)
			
		else
			script.Parent.Value.Value = 0
			
		end
		script.Parent.Bar.Size = UDim2.new(1, 0, ((1-((y-script.Parent.AbsolutePosition.Y)/850))), 0)
	end
end)
script.Parent.Value.Changed:connect(function()
	script.Parent.Bar.Size = UDim2.new(1, 0, (script.Parent.Value.Value/2)/100, 0)
end)

I Tried to do it but it doesn’t work it goes to the correct value but then back to 0 here is the script

mouse = false
local TweenService = game:GetService("TweenService")
script.Parent.MouseButton1Down:connect(function() mouse=true  end)
script.Parent.MouseButton1Up:connect(function()   mouse=false end)
script.Parent.MouseLeave:connect(function(x,y) mouse=false end)
script.Parent.MouseMoved:connect(function(x,y)
	if mouse then
		if 10-(((y-script.Parent.AbsolutePosition.Y)/850)*1) > 2 then
			script.Parent.Value.Value = 15-(((y-script.Parent.AbsolutePosition.Y)/850)*15)
			
		else
			script.Parent.Value.Value = 0
			
		end
		script.Parent.Bar.Size = UDim2.new(1, 0, ((1-((y-script.Parent.AbsolutePosition.Y)/850))), 0)
	end
end)
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
local tween = TweenService:Create(script.Parent.Bar, tweenInfo, {Size=UDim2.new(1, 0, (script.Parent.Value.Value/2)/100, 0)})
script.Parent.Value.Changed:connect(function()
	tween:Play()
end)

i keep changing things but nothing works

You will need to generate a new tween every time the value changes. If you combine your original script with the one you had been working on, you can get something like this:

local TweenService = game:GetService("TweenService")

local mouse = false
script.Parent.MouseButton1Down:connect(function() mouse=true  end)
script.Parent.MouseButton1Up:connect(function()   mouse=false end)
script.Parent.MouseLeave:connect(function(x,y) mouse=false end)
script.Parent.MouseMoved:connect(function(x,y)
	if mouse then
		if 10-(((y-script.Parent.AbsolutePosition.Y)/850)*1) > 2 then
			script.Parent.Value.Value = 15-(((y-script.Parent.AbsolutePosition.Y)/850)*15)

		else
			script.Parent.Value.Value = 0

		end
		script.Parent.Bar.Size = UDim2.new(1, 0, ((1-((y-script.Parent.AbsolutePosition.Y)/850))), 0)
	end
end)
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
script.Parent.Value.Changed:connect(function()
	TweenService:Create(script.Parent.Bar,tweenInfo,{Size = UDim2.new(1, 0, (script.Parent.Value.Value/2)/100, 0)}):Play()
end)

This code generates a UDim2 corresponding to the new size the bar should be just like your original code and generates the tween from this new size and runs it. This happens within the .Changed event handler rather than outside it.

In your second post, the tween is only ever generated the first time the code runs, which is why it doesn’t change even when the value changes.

1 Like

I can’t understand what you’re trying to achieve here? If you could provide some more context, maybe a gif so we can help you to the best of our ability.

1 Like

I tried this and this is what happened
https://gyazo.com/cf4b723bd07e389f3fa434fe2b5aaab0

Is the range of your values 0 to 15? If so, try this code:

local TweenService = game:GetService("TweenService")

local mouse = false
script.Parent.MouseButton1Down:connect(function() mouse=true  end)
script.Parent.MouseButton1Up:connect(function()   mouse=false end)
script.Parent.MouseLeave:connect(function(x,y) mouse=false end)
script.Parent.MouseMoved:connect(function(x,y)
	if mouse then
		if 10-(((y-script.Parent.AbsolutePosition.Y)/850)*1) > 2 then
			script.Parent.Value.Value = 15-(((y-script.Parent.AbsolutePosition.Y)/850)*15)

		else
			script.Parent.Value.Value = 0
		end
	end
end)
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
script.Parent.Value.Changed:connect(function()
	TweenService:Create(script.Parent.Bar,tweenInfo,{Size = UDim2.new(1, 0, script.Parent.Value.Value/15, 0)}):Play()
end)

This should scale the bar to whatever percentage of 15 your current value is, but I don’t know how well it will line up since the code seems to be dependent on screen size.

1 Like