Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
FiredDusk 1466 Moderation Voter
7 years ago

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

Log in to vote
4
Answered by 7 years ago
Edited 7 years ago

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).

Ad
Log in to vote
1
Answered by 7 years ago

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<number>

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<number>

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

Answer this question