How can I decrease my script length?

Here is my hideous code. Please tell me how I can decrease the script length.

FC.MouseButton1Down:Connect(function()
	if MarketPlaceService:PlayerOwnsAsset(Player.LocalPlayer, CLUB_INVESTOR) then
		DisplaySelectedClass.Text = "First Class Selected!"
		repeat
			wait(0.0000000000001)
			PE.Visible = false
			FC.Visible = false
			BC.Visible = false
			EC.Visible = false
			ReplaceEC.Visible = true
			ReplaceFC.Visible = true
			ReplaceBC.Visible = true
			ReplacePE.Visible = true
		until DisplaySelectedClass.Text == "First Class Selected!"
		PE.Visible = true
		FC.Visible = true
		BC.Visible = true
		EC.Visible = true
		ReplaceEC.Visible = false
		ReplacePE.Visible = false
		ReplaceFC.Visible = false
		ReplaceBC.Visible = false
	else
		DisplaySelectedClass.Text = "You don't own that class!"
		repeat
			wait(0.000000000000001)
			PE.Visible = false
			FC.Visible = false
			BC.Visible = false
			EC.Visible = false
			ReplaceEC.Visible = true
			ReplaceFC.Visible = true
			ReplaceBC.Visible = true
			ReplacePE.Visible = true
		until DisplaySelectedClass.Text == "You don't own that class!"
		PE.Visible = true
		FC.Visible = true
		BC.Visible = true
		EC.Visible = true
		ReplaceEC.Visible = false
		ReplacePE.Visible = false
		ReplaceFC.Visible = false
		ReplaceBC.Visible = false
	end
end)
1 Like

I’m thinking of maybe putting everything in a table and then setting it all at once but I’m not sure how to do that and I can’t find anything about it.

1 Like

You can create a table with the items, and then use a for loop to set everything at once.
Like this:

for index, item in pairs(tableName) do
   item.Visible = true
end
1 Like

What’s item? I’m pretty new to pairs.

1 Like

It’s just a variable, you can choose any name to it.
index is the index of the current table item, in this case it’s the item numerical position in the table.
item is the value of the current table item, in this case the instance that you are trying to set visible or invisible.
Here’s an example:

local tableTest = {
   ["hi"] = 1,
   ["what"] = 2,
   ["hello"] = 3,
}

for index, number in pairs(tableTest) do -- index and number are variables, so they can have any name!
   print(index, number)
end

This code will print to the output:

hi 1
what 2
hello 3

Okay so index is first and it’s the first one and the second one is pretty much self explanatory, correct?

1 Like

Yes, that’s right! If you don’t add an index (like this local table = { "hi" }) then it will just be the numerical position of the table item, in this case hi will have the index of 1.

1 Like

So I repeat the loop everytime I wanna use it again, right?

1 Like

Sure! It will decrease the script length a lot.

1 Like

Okay thanks, you really helped me learn this!

2 Likes

I mean the code does look like this: for index, item in pairs(tableClasses) do item.View = true for index, item in pairs(tableReplaceClasses) do item.View = false end end
but it is motivating me!

1 Like

In addition to this, it’s also recommended you instead use task.wait() from the task library as its more efficient and works with the task scheduler.

task.wait(0.0333) -- 1/30th of a second
task.wait(0.1667) -- 1/60th of a second

--[[
from what ive tested, this is the lowest wait time it can go
however, this is very dependent on the current performance of the machine
wait delays like these are only possible in the command bar
]]
task.wait(0.0008)
3 Likes

Okay, thank you, I’ll take it into consideration but I don’t think I’ll need it for what I’m making.

2 Likes

lua minifier will lower script length alot :+1:

2 Likes

also this can help

local function DisplaySelected(Text)
	DisplaySelectedClass.Text = Text
	repeat
		wait()
		PE.Visible = false
		FC.Visible = false
		BC.Visible = false
		EC.Visible = false
		ReplaceEC.Visible = true
		ReplaceFC.Visible = true
		ReplaceBC.Visible = true
		ReplacePE.Visible = true
	until DisplaySelectedClass.Text == Text
	PE.Visible = true
	FC.Visible = true
	BC.Visible = true
	EC.Visible = true
	ReplaceEC.Visible = false
	ReplacePE.Visible = false
	ReplaceFC.Visible = false
	ReplaceBC.Visible = false
end
FC.MouseButton1Down:Connect(function()
	if MarketPlaceService:PlayerOwnsAsset(Player.LocalPlayer, CLUB_INVESTOR) then
		DisplaySelected("First Class Selected!")
	else
		DisplaySelected("You don't own that class!")
	end
end)
1 Like

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