Ply:GetTable() help?

Hello I was wondering When I could actually use this, because I wanted to do something along the lines of this…

(Say in the init.lua file where your on initial spawn it does something along the lines of this)


ply:GetTable().Salary = 0

And so, when I get into the Shared.lua, I add something to see what the Salary is…


function GetSalary(ply)
		Msg(ply:Nick() .. "'s new salary is: " .. ply:GetTable().Salary)
--	return ply:GetTable().Salary
end
concommand.Add("GetSalary", GetSalary)

Whats wrong because this is the error I end up getting in game.


TestRP\gamemode\shared.lua:19: attempt to index field 'Salary' (a nil value) unable to concantenate value....

The error is just a slight remembrance of what it is…

Please need help :frowning:

Thanks <3

Don’t use GetTable( ) anymore unless you are debugging.

Just do this:



ply.Salary = 0


Okay Thanks for the help.

[editline]02:07AM[/editline]

Okay I get pretty much the same error…

Heres the Example GM:PlayerInitialSpawn function


function GM:PlayerInitialSpawn(ply)
ply.Salary = 0
end

and heres the Get Salary with the suggested fix…


function GetSalary(ply)
		Msg(ply:Nick() .. "'s new salary is: " .. ply.Salary)
end
concommand.Add("GetSalary", GetSalary)

However I still get this problem



TestRP\gamemode\shared.lua:19: attempt to concatenate field 'Salary' (a nil value)


[editline]02:21AM[/editline]

No one have any ideas? :G

[editline]02:37AM[/editline]

Okay So I Edited it a little bit to confirm that for some reason Salary is still an nil value for some reason… any ideas?


Msg(ply:Nick() .. "'s new salary is: " .. tostring(ply.Salary))

Output:

Test’s new salary is: nil

Try **[Entity.SetNetworkedInt

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Entity.SetNetworkedInt)** and **[Entity.GetNetworkedInt

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Entity.GetNetworkedInt)**

Im aware of Networked Variables, but I would prefer not to use them, because the more of them I use the more likely it crashes the server.

Atleast So I have been told.

You only use networked integers when you want to send stuff to the client. It can get rather expensive if you send to much data at once. That’s a buffer overflow, I believe.

As for your problem, try doing tostring(ply.Salary). If it prints nil, then I would use meta functions.

[lua]
local meta = FindMetaTable(“Player”)
function meta:GetMoney()
return self.Salary
end
function meta:SetMoney(money)
self.Salary = money
end[/lua]

Hm, I will try with the meta functions, that seems to be a little more promising just because i have had bad experiences with networked vars

[editline]03:07AM[/editline]

Obviously all these methods can’t be used in a client/server file. Can I have meta functions like getmoney() inside a file such as shared?

Tested with the meta function Idea…

Created new file for the meta functions and added this:


local meta = FindMetaTable("Player")  



--Salary
function meta:GetSalary()  
     return self.Salary  
 end  

however still in the function in the shared where I am calling this method it isn’t working, I assume its because I wouldn’t be able to since its in a completely different file?

Shared.lua:


function GetSalary(ply)
		Msg(tostring(meta:GetSalary()))
end
concommand.Add("GetSalary", GetSalary)

So I think I have narrowed down the problem, I just can’t fully put my finger on it.

function GM:PlayerInitialSpawn(ply)
ply.Salary = 0
end

This Apparently Doesn’t Save… Or.

When I set the salary equal to this…

	ply.Salary = TeamInfo.Citizen.Salary
	Msg(ply:Nick() .. "'s new salary is: " .. ply.Salary)

However the new salary does show up as the correct one (25)

Any suggestions?

You have to change the meta to ply.

Uh… I did in the more recent verison of that method, guess I forgot to post it…

function Salary(ply)
		Msg(tostring(ply:GetSalary()))
end
concommand.Add("GetSalary", Salary)

EDIT: however that still doesn’t fix the problem. There error I get is this…

Try moving the console command into a server side file and try it.

Now gives me an odd error… well not really odd just don’t understand why I am getting it :P.

created new file concommands.lua

already included it into init.lua

however now this is all that happens…



Unknown Command: 'GetSalary'


But it’s weird because it shows it in the little help stuff(when you type the letters it shows in the little drop down menu)…

Also tried moving to init.lua and the same effect ocurred.

[lua]function Salary(ply)[/lua] to [lua]function Salary( ply, cmd, args )[/lua]

Same problem as before…

doesn’t recognize the command.

Anyone have any ideas why adding a concommand thats recognized in the help, but not actually able to work “unknown command: Salary” Etc… Ive tried it in many different server files so who knows.

EDIT: Fixed, Figures that I had to put it in the same one was the file with all the meta functions in it. >.<