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

When/What are some useful ways to use tables?

Asked by
Pojoto 329 Moderation Voter
5 years ago

I understand how tables work and mostly everything about them, but I've just never come across a time where I needed to use them in scripts. I feel like there's probably somewhere in my scripts where instead of doing what I did, I could use a table and make things a lot easier and organized.

So I guess it'd be nice to include some examples that you've used tables in your scripts so I can get a better understanding of when to use tables for mine.

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

I will show you an example where you might need a table.

Let's say you had a group of parts. But you have multiple parts that you want to run code on. The other parts you don't. You'd do something like if part.Name == "Name1" or part.Name == "Name2" then, but that can get very messy. You'd use a type of table called a dictionary.

local parts = {
    ["Part1"] = true, -- you'd put the name of the part in between the [""]
    ["Part2"] = true,
    ["Part3"] = true,
    ["Part4"] = true, 
    ["Part5"] = true 
}

local children = workspace.Model:GetChildren() -- an example

for _, child in pairs(children) do -- loop through children 
    if child:IsA("BasePart") then -- check if they're a part
        if parts[child.Name] then -- if their name is in the dictionary! 
            child.BrickColor = BrickColor.Red()
            child.Transparency = 0.375
            child.Material = Enum.Material.Neon 
        end
    end
end
Ad

Answer this question