Alright, so I've been coding a small roleplaying gamemode and have been trying to send data to the client through usermessages. The usermessage arrives on the client but I can't seem to store onto the player because when I do I get this error: [code][RP\gamemode\cl_init.lua:13] attempt to index field 'RP' (a nil value)[/code] I don't seem to understand why the table isn't there when I had already defined it one line up from the error. Below are the files that makeup the gamemode:
init.lua
[lua]
AddCSLuaFile( "cl_init.lua" );
AddCSLuaFile( "sh_init.lua" );
include( "sh_init.lua" );
function GM:PlayerInitialSpawn( player )
self.BaseClass:PlayerInitialSpawn( player );
player:sendShort( "Test", 1337 );
end;
local playerMeta = FindMetaTable( "Player" );
function playerMeta:sendShort( key, value )
if ( not ValidEntity( self ) ) then
return;
end;
if ( not type( key ) == "string" ) then
return;
end;
if ( not type( value ) == "number" ) then
return;
end;
self.RP = self.RP or {};
self.RP[key] = value;
umsg.Start( "RP_Short" );
umsg.Entity( self );
umsg.String( key );
umsg.Short( value );
umsg.End();
print( "[SERVER]: Sending a short integer with a key of: " .. key .. " and a value of: " .. value .. "." );
print( "[SERVER]: Checking short integer: " .. self.RP[key] .. "." );
end;
[/lua]
cl_init.lua
[lua]
include( "sh_init.lua" );
function receiveShort( um )
local player = um:ReadEntity();
local key = um:ReadString();
local value = um:ReadShort();
print( "[CLIENT]: Receiving a short integer with a key of: " .. key .. " and a value of: " .. value .. "." );
player.RP = player.RP or {};
player.RP[key] = value;
print( "[CLIENT]: Checking short integer: " .. player.RP[key] .. "." );
end;
usermessage.Hook( "RP_Short", receiveShort );
[/lua]
sh_init.lua
[lua]
GM.Name = "RP";
GM.Author = "Adam Jones";
GM.Email = "";
GM.Website = "";
DeriveGamemode( "sandbox" );
[/lua]
As you can see, I've made some debug messages to see if the data was going to where it was supposed to properly. This helped me confirm that it was being received by the client but not stored on the player. Any help with this would be greatly appreciated.
player.RP is nil since you don't set the value anywhere
See, that's the thing, I set line 11 directly to a table (without the or bit) and it gives me the same error.
print player,key & value to make sure you are getting the correct data on client
Alright, just added a new message for receiving the player and it's returning NULL, why is that?
If you're trying to send an entity before InitPostEntity on the client it will not work. Try making a console command to run on the client after InitPostEntity to get it to send the data instead of wherever you're doing it now.
Turned it into a concommand and ran it manually when I spawned and it worked, thank you! I guess it was being sent before InitPostEntity like you said.
For future reference when debugging its always a good idea to use prints to see what the problem is before posting
[QUOTE=Czisi;28818125]Turned it into a concommand and ran it manually when I spawned and it worked, thank you! I guess it was being sent before InitPostEntity like you said.[/QUOTE]
I had this same problem about 3 months ago and I was puzzled, so I tried sending the EntIndex of the entity and adding it to a table, then trying to call the data from it using the index and it worked.
Sorry, you need to Log In to post a reply to this thread.