Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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 :)

This discussion is preserved as it was. New answers, comments, and votes are closed.