Declaring Clientside Table?

Am I doing something wrong? It’s supposed to declare the table on LocalPlayer() defined as selfPlayer however it isn’t working.




function inventoryHook(mess)

	local selfPlayer=LocalPlayer()
	
	if(selfPlayer.inventory==nil) then
		selfPlayer.inventory={}
	end
	local invInd=mess:ReadLong()
	local indEnt=mess:ReadEntity()
	selfPlayer.inventory[invInd]=indEnt
		
end


[editline]18th March 2011[/editline]

Also how would I get it again clientside?

You don’t need to put the table on the player client side, client side will always be just that one player.
Also it would help if you said how it’s not working, errors or messed up behavior and such.

Right, sorry I’m just getting frustrated sorry. But it gives me an error when I try to access an index of the table by using selfPlayer.inventory[0] in a function but it’s not working.

[editline]18th March 2011[/editline]

It generates this error; [Spies\gamemode\cl_init.lua:102] attempt to index field ‘?’ (a nil value)

I believe this to be the code that is necessary and relevant to know. :v



function inventoryHook(mess)

	local selfPlayer=LocalPlayer()
	
	if(selfPlayer.inventory==nil) then
		selfPlayer.inventory={}
	else
		table.insert(selfPlayer.inventory,indEnt)
	end
	local invInd=mess:ReadLong()
	local indEnt=mess:ReadEntity()
		
end

usermessage.Hook("InventorySend",inventoryHook)

function OpenAgentPanel()

	local inventoryWindow=vgui.Create("DFrame")
	inventoryWindow:SetPos(50,0)
	inventoryWindow:SetSize(ScrW()-50,ScrH())
	inventoryWindow:SetTitle("Myself...")
	inventoryWindow:SetVisible(true)
	inventoryWindow:SetDraggable(false)
	inventoryWindow:ShowCloseButton(true)
	inventoryWindow:MakePopup()
	
	
	local selfPlayer=LocalPlayer()
	
	if(selfPlayer.inventory!=nil) then
		local slot1=vgui.Create("SpawnIcon",inventoryWindow)
		slot1:SetPos(0,20)
		slot1:SetModel(selfPlayer.inventory[0]:GetModel())
		slot1.DoClick=function(btn)
		selfPlayer:ChatPrint("Class is: "..selfPlayer.inventory[0]:GetClass())
			local slot1Ops=DermaMenu()
			if(selfPlayer.inventory[0]:GetClass()=="spies_doc") then
				slot1Ops:AddOption("Check Destination",function()
				local myDest=selfPlayer.inventory[0]:GetVar("DestObj")
				selfPlayer:ChatPrint("Destination is: "..myDest:GetName()) end)
			end
		end
	end

end

concommand.Add("OpenAgentPanel",OpenAgentPanel)



which line is 102? and please use lua tags([noparse][lua][/lua][/noparse]) it will give line numbers, if it’s not the whole file then say which line equals which like this is lines 50 - 168, the more information the better.
Also you can try defining selfPlayer outside of the functions and use it in all of them.

[editline]18th March 2011[/editline]

Also it could be trying to access the table before you added any values if you make a table and set it to {}
value [0] will be nil and unusable till you set it. so besides checking for inventory to be nil you should check that the value you are trying to get is not nill also so check if selfPlayer.inventory[0] == nil before using it.

And… I just noticed in your usermessage hook, you try an assign a value to the table before you read it.
[lua]

function inventoryHook(mess)

local selfPlayer=LocalPlayer()

if(selfPlayer.inventory==nil) then
	selfPlayer.inventory={}  -- Giving no value
else
	table.insert(selfPlayer.inventory,indEnt)  -- VAlue isn't defined when we get here
end
local invInd=mess:ReadLong()  --Should be at top
local indEnt=mess:ReadEntity() -- Should be at top

end

usermessage.Hook(“InventorySend”,inventoryHook)
---------------------------------------------------- Old Above, New Below -----------------

function inventoryHook(mess)

local selfPlayer=LocalPlayer()  
local invInd=mess:ReadLong()  -- Read first 
local indEnt=mess:ReadEntity() -- Read First

if(selfPlayer.inventory==nil) then
	selfPlayer.inventory={ }  -- Only do this if there is no table
    end

--Check here to see you got good values from above before trying to insert into table.

    table.insert(selfPlayer.inventory, indEnt) -- But insert everytime

end

usermessage.Hook(“InventorySend”,inventoryHook)
[/lua]

If you missed it in the other thread

[lua]

Inventory = { }

usermessage.Hook( “SendItem”, function (data)
Item = {}

Item.Class = data:ReadString()
Item.Name = data:ReadString()
Item.Dec = data:ReadString()


Inventory[Item.Class] = Item

end)
[/lua]

also in the code above me why are you doing a check to see if the table exists it seems pointless just declare it at the top of a Clientside file and I don’t see the point of saving the data in a variable if you are only using it once

Neither seems to be working, any other ideas?

Its working perfectly for me I am using it right this second

I don’t even think it’s possible. :confused:

What isn’t possible?

Making an inventory system, not for me anyways. :confused:

I don’t understand what the problem is

Problem inventory is still nil after everything. :confused:

Poste the codes in pastebin.com, paste them like this:

code xyz.lua pastebin.com/adjasfdasd
code yz.lua pastebin.com/a232azsd

code z.lua pastebin.com/bvzxc

OR

.rar the gamemode and host it on mediafire.com

Then we talk about this.

Why is that necessary?

So we can help you.

Alright, fine. XP Just paranoid, but okay. Thanks for helping guys.

Link is: http://www.mediafire.com/?exs8dxc13irq7ru Try not to steal it. Not that the code’s anything worth stealing, but the idea… XP

Nerdpest, what you wanted was to use DATASTREAM.
Here you are, the altered files:
INIT.Lua: http://pastebin.com/qk0Sd7bT
CL_Init.lua: http://pastebin.com/rYLRYizZ (Check if I didn’t mistakenly named INIT as CL_INIT and backwards.)
Didn’t test, and also never messed with DATASTREAMs, but it might be working (cross the damn fingers!)

Datastream should not be used for this usermessages are much better in this situation and most situations

Usermessage is good for small stuff.
Datastream is good for big stuff.

If the player is a freacking addict, and his inventory is full of items of all sorts with all having big ammounts, you might break through the limit of 255bytes.

That is why you compress the data before you send it or use http://www.facepunch.com/threads/980338-User-messages-without-a-size-limit

Datastream should be avoided at all costs unless you really need to use it and you should never really need to