local g=game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")local R={}R.__index=R R.Cloner=script:WaitForChild("\083\108\105\100\101\114")local Z=.15 function R.new(g,Z,j,O,w)local h=setmetatable({},R)h.Value=0 h.Connections={}h.Destroy={}h.Bar=R.Cloner:Clone()h.Bar.Parent=Z h.Slider=h.Bar.Slider h.Min=j or 0 h.Max=O or 1 h.Round=w==nil and false or w h.Label=h.Bar:FindFirstChild("\076\097\098\101\108")or h.Slider:FindFirstChild("\076\097\098\101\108")h.Changed=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")table.insert(h.Destroy,h.Changed)table.insert(h.Destroy,h.Bar)if g then table.insert(h.Connections,h.Changed.Event:Connect(function(R,Z)g(R,Z)end))end h:Reconnect()return h,h.Changed.Event end function R.Set(Z,g,R)g=math.clamp(g,Z.Min,Z.Max)Z.Value=g local j=(g-Z.Min)/(Z.Max-Z.Min)Z:Update(j)if R then Z.Slider:TweenPosition(UDim2.new(j,0,.5,0),"\079\117\116","\076\105\110\101\097\114",.5,true)else Z.Slider.Position=UDim2.new(j,0,.5,0)end end function R.Update(R,g)local Z=R.Value local j=R.Min+(R.Max-R.Min)*g if R.Round then j=math.floor(j+.5)end R.Value=j if R.Label then R.Label.Text=tostring(j)end if R.Value~=Z then R.Changed:Fire(j,Z)end end function R.Reconnect(R)local j=R.Bar local O=R.Slider local w=j.AbsoluteSize;(j:GetPropertyChangedSignal("\065\098\115\111\108\117\116\101\083\105\122\101")):Connect(function()w=j.AbsoluteSize end)local h=j.InputBegan:Connect(function(g)if g.UserInputType==Enum.UserInputType.MouseButton1 or g.UserInputType==Enum.UserInputType.Touch then R.Listening=true end end)local U=O.InputBegan:Connect(function(g)if g.UserInputType==Enum.UserInputType.MouseButton1 or g.UserInputType==Enum.UserInputType.Touch then R.Listening=true end end)local i=g.InputEnded:Connect(function(g)if g.UserInputType==Enum.UserInputType.MouseButton1 or g.UserInputType==Enum.UserInputType.Touch then R.Listening=nil end end)local k=O.SelectionGained:Connect(function()R.Listening=true end)local Y=O.SelectionLost:Connect(function()R.Listening=nil end)local p=g.InputChanged:Connect(function(g)if R.Listening==true then local h if g.KeyCode==Enum.KeyCode.Thumbstick1 then local R=g.Position.X if math.abs(R)>Z then h=O.Position.X.Scale+(R>0 and.01 or-0.01)end elseif g.UserInputType==Enum.UserInputType.MouseMovement or g.UserInputType==Enum.UserInputType.Touch then local R,Z=j.AbsolutePosition,j.AbsolutePosition+w local O=g.Position local U=Z.X-R.X local i=O.X-R.X h=i/w.X end if h then h=math.clamp(h,0,1)O.Position=UDim2.new(h,0,.5,0)R:Update(h)end end end)table.insert(R.Connections,U)table.insert(R.Connections,h)table.insert(R.Connections,k)table.insert(R.Connections,p)table.insert(R.Connections,i)table.insert(R.Connections,Y)end function R.Disconnect(g)for R=1,#g.Connections,1 do local Z=g.Connections[R]Z:Disconnect()end end function R.Destroy(g)for R=1,#g.Destroy,1 do local Z=g.Destroy[R]Z:Destroy()end g.Destroy={}g:Disconnect()g=nil end return R
--[[
	local slider, changed_event = Slider.new(Function changed_function nil, GuiObject Parent nil, Number min 0, Number max 1, Boolean round false)
	
	slider:Disconnect() -- disconnect all input events
	
	slider:Reconnect() -- reconnect all input events
	
	slider:Destroy() -- destroy the slider UI and Object.
--]]

local UserInput = game:GetService("UserInputService")

local Slider = {}
Slider.__index = Slider
Slider.Cloner = script:WaitForChild("Slider")

local CONTROLLER_DEADZONE = 0.15

function Slider.new(changed_function, parent, map_min, map_max, round)
	local self = setmetatable({}, Slider)
	self.Value = 0
	self.Connections = {}
	self.Destroy = {}

	self.Bar = Slider.Cloner:Clone()
	self.Bar.Parent = parent
	self.Slider = self.Bar.Slider

	self.Min = map_min or 0
	self.Max = map_max or 1
	self.Round = round == nil and false or round

	self.Label = self.Bar:FindFirstChild("Label") or self.Slider:FindFirstChild("Label")

	self.Changed = Instance.new("BindableEvent")
	table.insert(self.Destroy, self.Changed)
	table.insert(self.Destroy, self.Bar)
	if changed_function then
		table.insert(
			self.Connections,
			self.Changed.Event:Connect(function(new_val, old_val)
				changed_function(new_val, old_val)
			end)
		)
	end
	self:Reconnect()

	return self, self.Changed.Event
end

function Slider:Set(value, do_tween)
	value = math.clamp(value, self.Min, self.Max)
	self.Value = value

	local percent = (value - self.Min) / (self.Max - self.Min)

	self:Update(percent)

	if do_tween then
		self.Slider:TweenPosition(UDim2.new(percent, 0, 0.5, 0), "Out", "Linear", 0.5, true)
	else
		self.Slider.Position = UDim2.new(percent, 0, 0.5, 0)
	end
end

function Slider:Update(percent)
	local old_val = self.Value
	local new_val = self.Min + ((self.Max - self.Min) * percent)
	if self.Round then
		new_val = math.floor(new_val + 0.5)
	end
	self.Value = new_val
	if self.Label then
		self.Label.Text = tostring(new_val)
	end
	if self.Value ~= old_val then
		self.Changed:Fire(new_val, old_val)
	end
end

function Slider:Reconnect()
	local bar = self.Bar
	local slider = self.Slider
	local bar_size = bar.AbsoluteSize
	bar:GetPropertyChangedSignal("AbsoluteSize"):Connect(function()
		bar_size = bar.AbsoluteSize
	end)
	local b_i_b = bar.InputBegan:Connect(function(io)
		if io.UserInputType == Enum.UserInputType.MouseButton1 or io.UserInputType == Enum.UserInputType.Touch then
			self.Listening = true
		end
	end)
	local s_i_b = slider.InputBegan:Connect(function(io)
		if io.UserInputType == Enum.UserInputType.MouseButton1 or io.UserInputType == Enum.UserInputType.Touch then
			self.Listening = true
		end
	end)
	local i_e = UserInput.InputEnded:Connect(function(io)
		if io.UserInputType == Enum.UserInputType.MouseButton1 or io.UserInputType == Enum.UserInputType.Touch then
			self.Listening = nil
		end
	end)
	local selection_g = slider.SelectionGained:Connect(function()
		self.Listening = true
	end)
	local selection_l = slider.SelectionLost:Connect(function()
		self.Listening = nil
	end)
	local mouse_movement = UserInput.InputChanged:Connect(function(io)
		if self.Listening == true then
			local percent
			if io.KeyCode == Enum.KeyCode.Thumbstick1 then
				local movement = io.Position.X
				if math.abs(movement) > CONTROLLER_DEADZONE then
					percent = slider.Position.X.Scale + (movement > 0 and 0.01 or -0.01)
				end
			elseif
				io.UserInputType == Enum.UserInputType.MouseMovement or io.UserInputType == Enum.UserInputType.Touch
			then
				local bar_start, bar_end = bar.AbsolutePosition, bar.AbsolutePosition + bar_size
				local mouse_pos = io.Position
				local distance = bar_end.X - bar_start.X
				local difference_mouse = mouse_pos.X - bar_start.X
				percent = difference_mouse / bar_size.X
			end
			if percent then
				percent = math.clamp(percent, 0, 1)
				slider.Position = UDim2.new(percent, 0, 0.5, 0)
				self:Update(percent)
			end
		end
	end)
	table.insert(self.Connections, s_i_b)
	table.insert(self.Connections, b_i_b)
	table.insert(self.Connections, selection_g)
	table.insert(self.Connections, mouse_movement)
	table.insert(self.Connections, i_e)
	table.insert(self.Connections, selection_l)
end

function Slider:Disconnect()
	for i = 1, #self.Connections do
		local c = self.Connections[i]
		c:Disconnect()
	end
end

function Slider:Destroy()
	for i = 1, #self.Destroy do
		local d = self.Destroy[i]
		d:Destroy()
	end
	self.Destroy = {}
	self:Disconnect()
	self = nil
end

return Slider
