OOP Slider Module

Hey. I have created a slider module that takes a container of a slider and then the slider button itself, and I would like feedback on it to see how it could be improved, or if I should add any features. Here is the source:

local slider = {}
slider.__index = slider

local uis = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local allSliders = {}

slider.debugMode = false

local function updateSlider(slider)
	local sliderFrame = slider.frame
	local sliderButton = slider.button
	local val = math.clamp((mouse.X-sliderFrame.AbsolutePosition.X)/sliderFrame.AbsoluteSize.X,0,1)
	sliderButton.Position = UDim2.fromScale(val, 0.5)
	slider.valueChanged:Fire(val)
	slider.currentValue = val
end

function slider.new(sliderFrame:Frame, sliderButton:GuiButton, identification:string)
	local slider = setmetatable(slider, {})
	slider.frame = sliderFrame
	slider.button = sliderButton
	slider.isSliding = false
	slider.isEnabled = true
	slider.isAlive = true
	slider.currentValue = 'Unassigned'
	slider.identification = identification
	
	sliderButton.MouseButton1Down:Connect(function()
		slider.isSliding = true
	end)
	
	uis.InputEnded:Connect(function(key, gpe)
		if key.UserInputType == Enum.UserInputType.MouseButton1 then
			slider.isSliding = false
		end
	end)
	
	slider.renderConnection = runService.RenderStepped:Connect(function()
		if not slider.isAlive then return end
		if slider.isSliding and slider.isEnabled then
			updateSlider(slider)
		end
	end)
	
	slider.valueChanged = Instance.new("BindableEvent")
	
	if slider.debugMode then
		warn(`New '{identification}' Slider`)
	end
	
	allSliders[identification] = slider
	return slider
end

function slider.getSlider(identification:string)
	return allSliders[identification]
end

function slider:Destroy()
	self.isAlive = false
	self.renderConnection:Disconnect()
	if slider.debugMode then
		warn(`Killed '{self.identification}' Slider`)
		allSliders[self.identification] = nil
	end
end

return slider

dont reupload line by line :stuck_out_tongue:

Anyways yeah just reply if I should add or fix stuff

I uploaded the module to the creator marketplace if yall wanna test it in game Slider - Creator Store (roblox.com)

1 Like

“dont steal”

oh man completely ruined my plans… :grimacing: cant steal it now… not like the code is right in front of me

(this is a joke)

1 Like

I think this is good. But I think they added UIDragDetectors now. But if Its for learning purposes. Its great!

2 Likes

lol i meant dont reupload, you can steal it

1 Like

disconnect events with the destroy function, and add some sort of check if a slider has been initialized, so there aren’t any repeating events

maybe

sliderFrame:SetAttribute(“Functionality”, “Slider”)

then guard clause
if sliderFrame:GetAttribute(“Functionality”) == “Slider” then return end

and remove attribute when destroy as well

also for performance reasons I would put the runService.RenderStepped:Connect(function() event inside the sliderButton.MouseButton1Down:Connect(function() and disconnect the render event inside the uis.InputEnded:Connect(function(key, gpe), so if you have for example 10 sliders it won’t run 10 times per frame when you aren’t doing anything.
The code looks good these are the only things I saw, anything else is just subjective to my style :+1:

1 Like