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
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
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.
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
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!
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!