Code Optimization

I think your Changed event can be changed to this, and shoudl still work the same

local fillParts = {}

for _, fill in ipairs(script.Parent:GetChildren()) do
	if not fill.Name:match("fill") then
		continue
	end
	table.insert(fillParts, fill)
end

FillVal.Changed:Connect(function()
	if FillVal.Value > #fillParts then
		FillVal.Value = #fillParts
		return
	end

	-- Set all fill parts to be invisible
	for _, fill in ipairs(fillParts) do
		fill.Transparency = 1
	end
	
	-- Stop here if FillVal is less or equal to 0
	if FillVal.Value <= 0 then
		FillVal.Value = 0
		return
	end
	
	-- Get the part with the value and make part visible
	script.Parent["fill" .. FillVal.Value].Transparency = 0
end)

@Judgy_Oreo Thank you for pointing out the mistake I did

We store the fillparts immediately in a table, then we first check in the changed event if our fillVal is greater than how many parts we have, if yes we changed the value and stop there. Then we make them all invisible, we stop there if value is less or equal to 0, finally we set that one specific part to be visible with the value

3 Likes