How would I go about cleaning this up?

How would I go about cleaning this up? Mainly the commented section:

local function gamepassHandler(player)
	local gp = Instance.new("Folder", player)
	gp.Name = "Gamepasses"
	
	local cm = Instance.new("BoolValue", gp)
	cm.Name = "CashMulti"
	
	local st = Instance.new("NumberValue", gp)
	st.Name = "Storage"
	--[[
	local tf = Instance.new("BoolValue", st)
	tf.Name = "25Storage"
	local ffty = Instance.new("BoolValue", st)
	ffty.Name = "50Storage"
	local svf = Instance.new("BoolValue", st)
	svf.Name = "75Storage"
	local db = Instance.new("BoolValue", st)
	db.Name = "100Storage"
	--]]
	local user = player.UserId
	
	local gamepassIDS = game:GetService("ReplicatedStorage"):WaitForChild("GamepassID")
	
	local gamepasses = {
		['25S'] = gamepassIDS:WaitForChild("25Storage").Value,
		['50S'] = gamepassIDS:WaitForChild("50Storage").Value,
		['75S'] = gamepassIDS:WaitForChild("75Storage").Value,
		['100S'] = gamepassIDS:WaitForChild("2xStorage").Value,
		['2xCash'] = gamepassIDS:WaitForChild("2xCash").Value
	}
	
	for name, id in pairs(gamepasses) do
		local hasPass
		
		local success, message = pcall(function()
			hasPass = MarketplaceService:UserOwnsGamePassAsync(user, id)
		end)
		
		if hasPass then
			print("Has Gamepass: "..id)
			if name == '25S' then
				tf.Value = true
			elseif name == '50S' then
				ffty.Value = true
			elseif name == '75S' then
				svf.Value = true
			elseif name == '100S' then
				db.Value = true
			elseif name == "2xCash" then
				cm.Value = true
			end
		end
	end
end

You can use a numeric loop with the step value of 25 to make it compact and readable just like the following code:

for i = 25, 100, 25 do
	local BoolObject = Instance.new("BoolValue", st)
	BoolObject.Name = i .. "Storage"
end

Edit: don’t forget to put them in a table to be able to reference them later in the code…

1 Like

Yeah, I went ahead and did that, thank you for pointing me in the right direction.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.