Inserting a table with a name value

Dear Community of Developers,

I have failed to express the issue of inserting a table using table.insert() without an index and instead with a string value in the following post. I have a table named Data and In my game I want to insert a table inside the Data table in the following format

["Name"] = {
	Time = {
		["Date and time"] = os.date("%c"),
		Timezone = os.date("%Z")
		},
	ID = Button.Name
}

Instead I got a table with an index value instead of a string value just like in the following example

[1] = {
	Time = {
		["Date and time"] = os.date("%c"),
		Timezone = os.date("%Z")
		},
	ID = Button.Name
}

I tried finding a way to add a name to my table, but failed to find necessary information for a clue. I posted my issue on the devforum but I did not have success in addressing the issue properly. So I came in conclusion to post another one in hopes of getting a solution

Thank you for any support given :smiley:!

1 Like

I may not be understandings correctly but you can just just do this:

local Table = {}

Table["Name"] = {} -- the table you want

I see some people suggested that in the other post, will this not work for you?

2 Likes

I did find this on the internet, and I did test it myself one more time. Sadly it didn’t work with this message

22:02:20.719  Workspace.Tycoons.Plots.Template.Objects.Buttons.Button Executor:64: attempt to index nil with 'Buttons'  -  Server - Button Executor:64  22:02:20.719
1 Like

The method I showed is the only real way to insert values into a dictionary. table.insert is only for arrays. The error you’re encountering is because it’s attempting to index something that doesn’t exist with Buttons. If you post the code from the Button Executor script, I may be able to help more.

1 Like

Thanks a lot for the help! Here’s the code you asked!

local Buttons
local Tycoon = script.Parent.Parent.Parent

local HttpService = game:GetService("HttpService")

local PlayerDataService = require(game.ServerScriptService.Data["Player Data"])

for _, Object in Tycoon.Objects:GetDescendants() do
	if Object.Name == "Owner Part" and Object.Parent.Name == "Door" and Object.Parent.Parent.Name == "Main" and Object:IsA("BasePart") then
		Object.Touched:Wait()
	end
end

task.wait(0.5)
local Player = game.Players:GetPlayerByUserId(tonumber(Tycoon:GetAttribute("Owner")))
local Data = PlayerDataService.GetData(Player)

for _, Floor in pairs(Tycoon.Objects.Floors:GetChildren()) do
	for _, Button in pairs(Floor.Purchased.Buttons:GetChildren()) do
		if Data.Tycoon.Floors[Floor.Name].Buttons[Button.Name] == nil or Data.Tycoon.Floors[Floor.Name].Buttons[Button.Name].ID == nil or Data.Tycoon.Floors[Floor.Name].Buttons[Button.Name].ID ~= Button.Name then
			Buttons = Tycoon.Objects.Floors[Floor.Name].Purchased.Buttons
			if Button:IsA("Script") then continue end
			--Properties
			local ButtonName = Button.Properties["Object Name"].Value
			local ButtonCost = Button.Properties.Cost.Value
			local ButtonBillboardFrame = Button.Structure.Head.BillboardGui.Area
			ButtonBillboardFrame["Object Name"].Text = tostring(ButtonName)
			ButtonBillboardFrame["Price Tag"].Text = "$"..ButtonCost

			if Button.Properties["Cash Generator"].Value == false then
				Button.Structure.Head.Color = Color3.new(1, 0, 0)
			else
				Button.Structure.Head.Color = Color3.new(0, 1, 0)
			end

			for _, Following in pairs(Button.Properties.Following:GetChildren()) do
				local ButtonID = tonumber(Following.Name)
				local ButtonObject = Buttons[tostring(ButtonID)]
				local ButtonObjectPrevParent = ButtonObject.Parent
				local ButtonGUID = HttpService:GenerateGUID(false)
				ButtonObject.Name = ButtonGUID
				ButtonObject.Parent = game.ReplicatedStorage.Tycoons.tmp

				Button.Structure.Head.Touched:Connect(function(hit)
					local Player: Player = game.Players:GetPlayerFromCharacter(hit.Parent)
					if Tycoon:GetAttribute("Owner") == tostring(Player.UserId) then
						ButtonObject.Parent = ButtonObjectPrevParent
						ButtonObject.Name = tostring(ButtonID)
					end
				end)
			end

			--Functionality
			local ObjectLink = Button.Properties["Object Link"].Value
			local ObjectLinkPrevParent = ObjectLink.Parent
			local ObjectLinkPrevName = ObjectLink.Name
			ObjectLink.Name = HttpService:GenerateGUID(false)
			ObjectLink.Parent = game.ReplicatedStorage.Tycoons.tmp

			Button.Structure.Head.Touched:Connect(function(hit)
				local Player: Player = game.Players:GetPlayerFromCharacter(hit.Parent)
				local Date = DateTime.now():ToLocalTime()
				if Tycoon:GetAttribute("Owner") == tostring(Player.UserId) then
					Data.Tycoon.Floors[Floor].Buttons[Button.Name] = {
						Time = {
							["Date and time"] = os.date("%c"),
							Timezone = os.date("%Z")
						},
						ID = Button.Name
					}
					Button:Destroy()
					ObjectLink.Name = ObjectLinkPrevName
					ObjectLink.Parent = ObjectLinkPrevParent
				end
			end) 
		else
			Button:Destroy()
		end
	end
end

The error comes from here and is caused because Floors[Floor] is not

image

If you add this code (just the print), it should give us some more insight

image

Thank you so much for your consideration in helping! The print function returned the exact table contents the floors table had and it also gave out the index of each individual floor! Here is what it returned. In this case I only had one floor, but it is necessary to loop through all the floors to continue the game’s progress!

  22:27:50.300   {
                    ["1"] =  {
                       ["Buttons"] = {}
                    }
                 } 1  -  Server - Button Executor:64

Sorry for the late reply, the issue is because the 1 printed is an instance and the 1 in the table is a string. Trying making it

image

Only difference is changing Floor to Floor.Name

1 Like

Holy! I thought that the Floor was not an issue since I misread the error message and I thought that the Buttons inside the Floor table was not found, not the actual Floor table! Thank you so much! I should be the one sorry for the late response, due to the fact that I live in different timezones!

1 Like

Stuff like this has happened to anyone doing it long enough XD

1 Like