Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make this code in a short amount of lines?

This is just a script in a gui and Idk how to make it a better way.

 script.Parent.Parent.Car1.Visible = false 
 script.Parent.Parent.Car2.Visible = false 
 script.Parent.Parent.Car3.Visible = false 
 script.Parent.Parent.Car4.Visible = false 
 script.Parent.Parent.Car5.Visible = false 
 script.Parent.Parent.Car6.Visible = false 
 script.Parent.Parent.Car7.Visible = false 
 script.Parent.Parent.Car8.Visible = false 
 script.Parent.Parent.Car9.Visible = false 
 script.Parent.Parent.Car10.Visible = false

2 answers

✓ Accepted answer

The wonderful world of for loops, where anything is possible!

for i = 1, 10 do
	script.Parent.Parent["Car" .. i].Visible = false
end

This type of for loop will start at 1 and increase to 10, storing the value in i. There are also generic for loops (for a, b, c, ... in func, val1, val2, val3, ... do), but they need a little more explaining (and are not what you need at the moment).

I'm not realy sure if there are ONLY Items named Car with a number, so here are two scripts depending on that.

If there are items named other than Car

validnames = {"Car1", "Car2","Car3", "Car4", "Car5", "Car6", "Car7", "Car8", "Car9", "Car10"}

for i,v in ipairs(script.Parent.Parent:GetChildren()) do
	if validnames[v.Name] then
		v.Visible = false
	end
end

If there are ONLY items named Car

for i,v in ipairs(script.Parent.Parent:GetChildren()) do
		v.Visible = false
end

This discussion is preserved as it was. New answers, comments, and votes are closed.