• Lua error with tables and for cycle
    7 replies, posted
Sup. I am making very simple entity for my very own DarkRP server. And I'm getting [CODE]rptrash_big/cl_init.lua:50: bad key to string index (number expected, got string)[/CODE] error. This is the code: [CODE]include('shared.lua') local Container local Frame local CarriedItems = {} local function introWindow() Frame = vgui.Create("DFrame") Frame:SetSize(320,200) Frame:SetPos(ScrW()/2-200,ScrH()/2-200) Frame:SetTitle("Помойка") Frame:SetVisible(true) Frame:SetDraggable(true) Frame:ShowCloseButton(true) Frame:MakePopup() Frame.Paint = function(self,w,h) draw.RoundedBox(0,0,0,w,h,Color(10,10,10,150)) end if table.Count( CarriedItems ) == 0 then Container = vgui.Create("DPanel") Container:SetParent(Frame) Container:SetSize(300,160) Container:SetPos(10,30) local MyName = vgui.Create("DLabel") MyName:SetParent(Container) MyName:SetText("Тут пусто") MyName:SizeToContents() MyName:SetTextColor(Color(0, 0, 0, 255)) MyName:SetPos(5, 5) end if table.Count( CarriedItems ) != 0 then local TheListPanel = vgui.Create( "DPanelList", Frame ) TheListPanel:SetSize(310,160) TheListPanel:SetPos( 5, 30 ) TheListPanel:EnableVerticalScrollbar( true ) TheListPanel:EnableHorizontal( true ) TheListPanel.Paint = function(self,w,h) draw.RoundedBox(0,0,0,w,h,Color(255,255,255,255)) end for k,v in pairs(CarriedItems) do local entFrame = vgui.Create( "DPanel" ) entFrame:SetSize( TheListPanel:GetWide(), 60 ) entFrame.Paint = function( self, w, h ) surface.SetDrawColor( Color( 242, 242, 242 ) ) surface.DrawLine( 0, h - 1, w, h - 1 ) --draw.SimpleText( string.upper(v["Name"]), "Helvetica24", 56, 14, Color( 0, 0, 0 ) ) print("k = "..tostring(k).." v = "..tostring(v).." trying to resolve smth as v.name" .. tostring(v.Name)) -- line 50 here end end end end net.Receive( "CarriedItems", function(len) CarriedItems = net.ReadTable() introWindow() end ) [/CODE] CarriedItems (they are passed to client as these via net.-shit. Original names are AllowedItems) are defined in shared.lua as [CODE]AllowedItems = {} AllowedItems["item_p229"] = { Name = "P 229", Description = "P229", Model = "models/weapons/w_sig_229r.mdl", Entity = "m9k_sig_p229r" } AllowedItems["item_m92beretta"] = { Name = "M9 Beretta", Description = "M9 Beretta", Model = "models/weapons/w_beretta_m92.mdl", Entity = "m9k_m92beretta" } AllowedItems["m9k_colt1911"] = { Name = "Colt 1911", Description = "Colt 1911", Model = "models/weapons/s_dmgf_co1911.mdl", Entity = "m9k_colt1911" } [/CODE] Any ideas why? :c
Bump. No ideas?
Remove print, see if the error still occurs. What the error is saying is that something is returning a string when it needs to be an int.
If I'm reading this correctly, when you call tostring(v) on line 50, you're trying to turn a table into a string and print it. If removing the print fixes the error, try putting it back without [CODE]" v = "..tostring(v)..[/CODE] If it doesn't error, try PrintTable(v) instead. Good luck, I'll check back.
This is getting pretty funny. Tried to do this: [CODE] table.foreach( CarriedItems, function( key, value ) local entFrame = vgui.Create( "DPanel" ) entFrame:SetSize( TheListPanel:GetWide(), 60 ) entFrame.Paint = function( self, w, h ) surface.SetDrawColor( Color( 242, 242, 242 ) ) surface.DrawLine( 0, h - 1, w, h - 1 ) --draw.SimpleText( string.upper(v["Name"]), "Helvetica24", 56, 14, Color( 0, 0, 0 ) ) print("k = "..tostring(key).." v = "..PrintTable(value)) end end)[/CODE] and getting [CODE]k = 1 v = table: 0xe9cf6e08[/CODE] btw this is infinite cycle... But when i try to [CODE] table.foreach( CarriedItems, function( key, value ) table.foreach( value, function( key, value ) local entFrame = vgui.Create( "DPanel" ) entFrame:SetSize( TheListPanel:GetWide(), 60 ) entFrame.Paint = function( self, w, h ) surface.SetDrawColor( Color( 242, 242, 242 ) ) surface.DrawLine( 0, h - 1, w, h - 1 ) --draw.SimpleText( string.upper(v["Name"]), "Helvetica24", 56, 14, Color( 0, 0, 0 ) ) print("k = "..tostring(key).." v = "..tostring(value)) end end) end)[/CODE] It says [ERROR] gamemodes/darkrp/entities/entities/rptrash_big/cl_init.lua:54: bad argument #1 to 'foreach' (table expected, got string) Damn, any suggestions? This is really pissing me off.
Why did you change the formatting from for k, v in pairs() to table.foreach()? I'm just wondering. Since it's giving you an error as to the arguments of foreach, and simple for loops don't take arguments, changing it back may either fix it or give a more clear error. 0xe9cf6e08 would have been what I expected when you tried the tostring(), instead of the error in your OP. I've never personally used table.foreach, but I would suggest changing it back to a simple for loop, at least for temporary debugging purposes, and see what the results are with the PrintTable. I'm assuming removing the print all together from the OP stopped the error?
[QUOTE=ZephyrWarrior;46734520]Why did you change the formatting from for k, v in pairs() to table.foreach()? I'm just wondering. Since it's giving you an error as to the arguments of foreach, and simple for loops don't take arguments, changing it back may either fix it or give a more clear error. 0xe9cf6e08 would have been what I expected when you tried the tostring(), instead of the error in your OP. I've never personally used table.foreach, but I would suggest changing it back to a simple for loop, at least for temporary debugging purposes, and see what the results are with the PrintTable. I'm assuming removing the print all together from the OP stopped the error?[/QUOTE] Yes. And it seems that I found error. I've been adding items to CarriedItems in init.lua which had [I]table.insert( CarriedItems, tostring(table.Random(AllowedItems)))[/I] code that was adding [I]the table: 0x....[/I] instead of exact table. I removed the tostring function, but now there's new error. [QUOTE][ERROR] gamemodes/darkrp/entities/entities/rptrash_big/init.lua:28: bad argument #2 to 'insert' (number expected, got table) [/QUOTE] Now I'm wondering of how to add table, because it seems that table.insert function can not insert tables inside tables. Any ideas? Thx in advance.
Can you post the modified complete code?
Sorry, you need to Log In to post a reply to this thread.