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

roblox hwo to make a table with 2 values ?

Asked by 7 years ago
Edited 7 years ago

so i wanna make a load system for customizable houses. to do that i need 2 values the name of the brick and the cframe.

but i dont kopw how to store this in a table like i want a table with the nam bricks and when u use the print commmand like

print(bricks[1])

it will output:

Part, 125,154,456

(i used random numbers for the cframe)

someone knows how to do this ?

0
what is bricks?? AstrealDev 728 — 7y

1 answer

Log in to vote
3
Answered by
Azarth 3141 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

If you mean you simply want to insert two values into one table, then you'd use table.insert. That way you can still utilize tabl[numhere].

local tabl = {}
table.insert(tabl, 'PartName')
table.insert(tabl, CFrame.new(0,0,0))

print(tabl[1], tabl[2])

Otherwise, if you wanted a table for each house, you'd need a different approach. Either you can use a dictionary type of style which is based on a key pair relationship or an array using table.insert.

A dictionary would be a key and a value. The key is the name of the table and the value is the table. Example: ['Azarth'] = {PlaysROBLOX = true, SpeaksRussian = false}

local tabl = {}

tabl['house1'] = { -- State the table name. In brackets name the key = the table.
    partName = 'Part1', 
    position = CFrame.new(0,0,0) }

-- You could then access house1 by name
print(tabl['house1'].position)

-- or for loop
for i,v in pairs(tabl) do 
    -- i is the name (key) and v is the table.
    print(i, v.position)
end

You could also insert a table within a table. This benefits you if you want the tables to be in the order (like a player-list) you inserted them as dictionaries are not when you try and return them in a for loop. Also beneficial if you want to nest other tables inside your main entry point.

local mainTable = {}

do
    local entry = {}
    entry.id = "house1"
    entry.partName = 'Part1'
    entry.position = CFrame.new(0,0,0)
    table.insert(mainTable, entry)
end

--[[ Visually, the table will then look like this 
mainTable = {  
    { id = 'house1', partName = 'Part1', position = CFrame.new(0,0,0) }  
}
--]]


-- You can then access house1 with a for loop
for i = 1, #mainTable do 
    if mainTable[i].id == "house1" then 
        print(mainTable[i].position)
    end
end

-- or
for i,v in pairs(mainTable) do
    if v.id == "house1" then 
        print(v.position)
    end
end

Ad

Answer this question