• Inserting data into a table within a table.
    9 replies, posted
I'm new to all this coding stuff so i'm not really sure how to do this. Here is the code: table.insert(TP.PROJECTLIST,{ projectName = projectName, projectProgress = projectProgress, projectDeadline = projectDeadline, projectTeam = projectTeam, projectMembers = table.insert(TP.PROJECTLIST[index].projectMembers,LocalPlayer():SteamID()), --Im attempting to add the players steamid to the projectMembers table within the TP.PROJECTLIST table. })
This doesn't make any sense, how do you want to overwrite something with table.insert?
I'm not sure what you mean? I changed the commented text to be more specific as to what I wanted to not overwrite is that answers your question.
What is your full code?
You'd have to do that outside the initial table.insert call. table.insert(TP.PROJECTLIST,{ projectName = projectName, projectProgress = projectProgress, projectDeadline = projectDeadline, projectTeam = projectTeam, }) -- make sure the table actually exists TP.PROJECTLIST.projectMembers = TP.PROJECTLIST.projectMembers or {} table.insert(TP.PROJECTLIST.projectMembers, Player:SteamID())
function joinProject(selected,name,index) if canopen == 1 then local panel = vgui.Create("DFrame") canopen = 0 panel:SetSize(300,75) panel:Center() panel:SetTitle("Are you sure?") panel:MakePopup() panel:SetDraggable(false) panel:SetBackgroundBlur(true) local lbl = vgui.Create("DLabel",panel) lbl:SetText("Are you sure you want to join the project '"..name.."'?") lbl:SetPos(0,30) lbl:SizeToContents() lbl:CenterHorizontal() local ye = vgui.Create("DButton",panel) ye:SetSize(100,20) ye:SetPos(30,50) ye:SetText("Yes") --print("Table: "..util.TableToJSON(TP.PROJECTLIST[index]["projectMembers"])) function ye:DoClick() if team.GetName(LocalPlayer():Team()) == selected then if TP.PROJECTMEMBERS[LocalPlayer():SteamID()] == name then LocalPlayer():ChatPrint("Unable to join project! Your already a member of '"..name.."'.") else LocalPlayer():ChatPrint("You joined project '"..name.."'!") table.insert(TP.PROJECTLIST,{ projectName = projectName, projectProgress = projectProgress, projectDeadline = projectDeadline, projectTeam = projectTeam, projectMembers = table.insert(TP.PROJECTLIST[index].projectMembers,LocalPlayer():SteamID()), }) TP.PROJECTMEMBERS[LocalPlayer():SteamID()] = name net.Start("tp_saveMember") net.WriteString(util.TableToJSON(TP.PROJECTMEMBERS)) net.SendToServer() end else LocalPlayer():ChatPrint("Unable to join project! Team '"..selected.."' required.") end canopen = 1 panel:Close() end local no = vgui.Create("DButton",panel) no:SetSize(100,20) no:SetPos(170,50) no:SetText("No") function no:DoClick() canopen = 1 panel:Close() end end end
table.insert returns the index that the item was inserted into. Use this to your advantage. --Add a project to the project list. local i = table.insert(TP.PROJECTLIST,{ projectName = projectName, projectProgress = projectProgress, projectDeadline = projectDeadline, projectTeam = projectTeam, projectMembers = {}, --Im attempting to add the players steamid to the projectMembers table within the TP.PROJECTLIST table without overwriting the data already in the projectMembers table. }) --When you want to add a player to that project: table.insert(TP.PROJECTLIST[ i ].projectMembers,LocalPlayer():SteamID())
table.insert(TP.PROJECTLIST[index].projectMembers,LocalPlayer():SteamID()) table.insert(TP.PROJECTLIST,{ projectName = projectName, projectProgress = projectProgress, projectDeadline = projectDeadline, projectTeam = projectTeam, projectMembers = TP.PROJECTLIST[index].projectMembers }) That would do what you want, but there is a lot more wrong with the code that you could be doing better. Your whole structure is not good
Thanks that worked! I'm fairly new to GLua and coding in general so what can I do better with my code?
Just some general tips, if you're setting `canopen = 1`, you can use true or false instead. Then you don't have to check `if canopen==1`, you can just do `if canopen` Instead of using net.WriteString(util.TableToJSON(table)) you can use net.WriteTable(table). It's more network efficient in best-case scenarios. And it's unclear but if you're storing things by name (projects, players, or what-have-you) you should consider storing them by some kind of unique ID. SteamID works for players, but for your own custom things you can assign them some kind of index. That's just good design though, if it works it works. Also, consider using `local function joinProject()` so other addons don't have conflicts with yours.
Sorry, you need to Log In to post a reply to this thread.