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

How do I use 'local'?

Asked by
Sam4550 42
9 years ago

I want to shorten my script by making one of those things: local buttons = script.Parent.Parent.Button1 I also want to be able to use buttons for Button2 and Button3. How do I have multiple things under one local thingy?

Thanks, - Sam4550

1
This isn't really the question you're asking; you're asking about something else. TheLuaWeaver 301 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

You're looking for tables. You can put

local buttons={script.Parent.Parent.Button1,script.Parent.Parent.Button2,script.Parent.Parent.Button3}

and then, later on use

buttons[1]
buttons[2]
buttons[3]

Of course, there are better way to do this. To shorten your code more, you can do

local container=script.Parent.Parent
local buttons={container.Button1,container.Button2,container.Button3}

Or

local buttons={}
for i=1,3 do
    buttons[i]=script.Parent.Parent["Button"..i]
end

Or, if the buttons are the only thing in the container,

local buttons=script.Parent.Parent:GetChildren()

Then, later on, instead of repeating code 3 times, you can do

for number,button in pairs(buttons) do
    button.Text="Example"
end

If you're having trouble understanding that last bit, you can read about the generic for loop.

0
Thanks, it seems to make sense. I'll just try using it first. Sam4550 42 — 9y
Ad

Answer this question