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

Is there a way to add a table to another table?

Asked by 6 years ago
Edited 6 years ago

Ex: I am trying to add something in table1 to table2 then print the results In lines 30, 42,48 are where I use this example it adds xpLvlTable to the table that is stored in 'DataStore'

-- Variables
local xp_lvlDataStore = game:GetService('DataStoreService'):GetDataStore('xp/lvlStore')
local repStorage = game:GetService('ReplicatedStorage')
local xpChanger = repStorage:WaitForChild('Xp_LvlChanged')
local xpToAdd = 5
local part = script.Parent
local maxLvl = 350

-- Events
pcall(part.ClickDetector.MouseClick:Connect(function(plr)
    -- Defines xp and lvl
    local key = 'Player-'..plr.userId
    local xpLvlTable = xp_lvlDataStore:GetAsync(key)
    local curXp = xpLvlTable[1]
    local curLvl = xpLvlTable[2]
    print('serverCurXp = '..curXp)
    print('serverCurLvl = '..curLvl)
    -- Fire to player
    xpChanger:FireClient(plr,xpToAdd)
    -- Math
    local newLvl = curLvl + 1
    local newXp = curXp + xpToAdd
    local lvlEquation = 10*curLvl
    print('ServerNewXp = '..newXp)
    -- Adds lvls and sets xp
    if curLvl == 0 and newXp >= 10 then
        print('Saving 1st lvl')
        local new_xpLvlTable = {1,(xpToAdd - 10)}
        wait(6)
        xp_lvlDataStore:IncrementAsync(key,(new_xpLvlTable))
        print('Saved')
    elseif curLvl >= maxLvl then
        print('Saving maxLvl')
        local xpLvlTable = {0,350}
        wait(6)
        xp_lvlDataStore:SetAsync(key,(xpLvlTable))
        print('Saved')
    elseif lvlEquation <= newXp and lvlEquation ~= 0 then
        print('Saving Lvled Up')
        local xpLvlTable = {1,xpToAdd - lvlEquation}
        wait(6)
        xp_lvlDataStore:IncrementAsync(key,(xpLvlTable))
        print('Saved')
    else
        print('Saving Did not lvl up')
        local xpLvlTable = {curLvl,xpToAdd}
        wait(6)
        xp_lvlDataStore:IncrementAsync(key,(1))
        print('Saved')
    end
end))

I meant to use one table and its numbers that are in it to be added to the numbers inside the second table

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Metatables

http://robloxdev.com/articles/Metatables

-In short they are tables that go inside other tables, they also have more "power" than regular tables as they can execute functions.

Ad
Log in to vote
1
Answered by
chomboghai 2044 Moderation Voter Community Moderator
6 years ago

Yep. Just add the table as you would for any other element.

local table1 = {"apple", "banana", "pommegranate", "idk if that spelling is right"}
local table2 = {1, 3, "hello", table1}

-- same thing as saying

table2 = {1, 3, "hello", {"apple", "banana", "pommegranate", "idk if that spelling is right"}}

-- to get table1 from table2
table1 = table2[4] -- table is the 4th index

-- to get the first element of table 1 from table 2
local firstElement = table2[4][1] --> "apple"

-- alterantively
table1 = table2[4]
firstElement = table1[1] --> "apple"

Hope this helps! :)

0
I meant to use one table and its numbers that are in it to be added to the numbers inside the second table RichCookieGamer 7 — 6y
0
Added as in operationally? Like 3 + 5 so the result is 8? Or add as in concatenate the contents of the table? chomboghai 2044 — 6y

Answer this question