Help me finish this... (Question about loops and tables)


PanelTable = {}


function CreatePanel()
	PanelTable.base = vgui.Create("DFrame")
	PanelTable.base:SetSize(1800, 1000)
	PanelTable.base:SetPos(ScrW() / 2 - PanelTable.base:GetWide() / 2, ScrH() / 2 - PanelTable.base:GetTall() / 2)
    PanelTable.base:ShowCloseButton(true)
	PanelTable.base:MakePopup()
	PanelTable.base:SetVisible(true)
	PanelTable.base:SetDraggable(true)
	PanelTable.base:SetTitle("Instructor Panel")
	
	
	MyTabs = vgui.Create( "DPropertySheet",PanelTable.base )
	MyTabs:SetPos( 10, 30 )
	MyTabs:SetSize( 1700, 900 )
	MyTabs:AddSheet( "Main", FirePanel, nill, false, false, "Fire here" )	
		
		FirePanel = vgui.Create("DPanel",MyTabs)		
		FirePanel:SetSize(1700, 900)
		FirePanel:SetPos(10,30,1)
		FirePanel:SetDrawBackground(  		false ) 
					
		PanelTable.Button1	= vgui.Create( "DButton", FirePanel )
		PanelTable.Button1:SetSize(200, 100)
		PanelTable.Button1:SetPos(10,10,3)
		PanelTable.Button1:SetText( "Button 1" )
		PanelTable.Button1.DoClick = function (self)
									self.Pressed = true 
									self.TimePressed = os.time()									
		
		end
					
		PanelTable.Button2	= vgui.Create( "DButton", FirePanel )
		PanelTable.Button2:SetSize(200, 100)
		PanelTable.Button2:SetPos(220,10,3)
		PanelTable.Button2:SetText( "Button 2" )
		PanelTable.Button2.DoClick = function (self)
									self.Pressed = true 
									self.TimePressed = os.time()
		
		end	
					
		PanelTable.Button3	= vgui.Create( "DButton", FirePanel )
		PanelTable.Button3:SetSize(200, 100)
		PanelTable.Button3:SetPos(440,10,3)
		PanelTable.Button3:SetText( "Button 3" )
		PanelTable.Button3.DoClick = function (self)
							self.Pressed = true
							self.TimePressed = os.time()
							PrintTable (button) // trying to find out what is happening 
                                                        PrintTable (pressedbuttons) 
							
										
		end
	
	
	
	
										// PanelTable.button1 = vgui.Create("DButton", PanelTable.base)
	
panels = PanelTable.base:GetChildren() -- Change panel here

if panels then
	for k, v in pairs(panels) do
		if v:GetClassName() == "DButton" then
			v.Pressed = false

			if not v.DoClick then
				v.DoClick = function(self)
					self.Pressed = true
					self.TimePressed = os.time()
				end
			end
		end
	end
end	
	
local pressedbuttons = {}

for k, v in pairs(panels) do
	if v:GetClassName() == "DButton" then
		local button = { v:GetText() }

		if v.Pressed and v.TimePressed then
			table.insert(button, v.TimePressed)
		end

		table.Add(pressedbuttons, button)
	end
end


	-- Do more stuff here
end

local buttonpressedtable = { -- This is the table format to follow
	{"ButtonOne", TIME},
	{"ButtonTwo", TIME},
	{"ButtonThree"} -- This one was not pressed
}
-- Also, TIME should be a value of os.time

local TextToWrite = ""
for k, v in pairs(PanelTable) do
	if v[2] and isnumber(v[2]) then
		TextToWrite = TextToWrite .. "[" .. os.date("%X - %d/%m/%Y", v[2]) .. "]" .. "The button " .. v[1] .. " was pressed
"
	else
		TextToWrite = TextToWrite .. "[NONE] The button " .. v[1] .. " was not pressed
"
	end
end

function ClearTable()
	PanelTable = {}
end 

concommand.Add('instructorpanel',CreatePanel)

This is a derma panel with buttons which are supposed to be pressed or not pressed and logged accordingly. (ongoing question for several weeks). With help I’ve gotten it this far. (Scrapped original and started from scratch, making it small).

However, I keep getting

When I try PrintTable of the (button) table inside the loop (at the DoClick) it says table expected, got userdata.

Is that because o.s.time is userdata? If it is a number, what is the problem? I thought the formatting was fine. Help me make the connection please.

Make sure you wrap all panels loops with “if panels then”

Thanks… got past that to find this:

The line (121) in question is


TextToWrite = TextToWrite .. "[NONE] The button " .. v[1] .. " was not pressed
"

from the code block


local TextToWrite = ""
if panels then
for k, v in pairs(PanelTable) do
	if v[2] and isnumber(v[2]) then
		TextToWrite = TextToWrite .. "[" .. os.date("%X - %d/%m/%Y", v[2]) .. "]" .. "The button " .. v[1] .. " was pressed
"
	else
		TextToWrite = TextToWrite .. "[NONE] The button " .. v[1] .. " was not pressed
"
	end
end
end
end
function ClearTable()
	PanelTable = {}
end 

removing local from TextToWrite did not change anything either. Also tried pulling this function out of the CreatePanel function but that just gave me an error on the local panels = PanelTable:GetChildren() line.