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


local UserInput = game:GetService("UserInputService")

local Slider = {}
	Slider.__index = Slider
	Slider.Cloner = script:WaitForChild("Slider")
	
	local CONTROLLER_DEADZONE = .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,.5,0), "Out", "Linear", .5, true)
		else
			self.Slider.Position = UDim2.new(percent,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 + .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 .01 or -.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,.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
