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

How to insert a name and a value into a table?

Asked by
dirk2999 103
5 years ago

I have a bunch of boolValues and IntValues I would like to insert into a table to save. The end game is to have something like this,

local new_Save = {
    ["Points"] = 0,
    ["last_Save"] = nil,
    ["daily_Reward"] = nil,
    ["BaR"] = false,
    ["Booster"] = false,
    ["McDonalds"] = false,
    ["OMG"] = false,
    ["WhiteandBlack"] = false,
    ["Winter"] = false
}

How do I start from this

local new_Save = {

}

and using table.insert insert from a group of bool and int values?

1 answer

Log in to vote
0
Answered by
chomboghai 2044 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

You cannot insert a value into a table with a non integer key. This means table.insert(new_Save, "Points", points) is not valid.

One solution is to simply make a function that takes in every key as the parameters and returns the object back.

local function getSaveObject(points, lastsave, dailyreward, bar, booster, mcdonalds, omg, whiteandblack, winter)
    local t = {
        ["Points"] = points
        ["last_Save"] = lastsave
        ["daily_Reward"] = dailyreward
        -- ... continue
    }
    return t
end

local new_Save = getSaveObject(0, nil, nil, false, false, -- etc.)

That is one method you could use and I hope it helps.

EDIT:

Explaining how you are receiving the integer and bool values would help me provide a more specific answer :)

Ad

Answer this question