Getting the player entity?

I’ve been picking away at a simple gamemode, but ran into a little problem…

I have a file called ‘Resources.lua’ but I need the player to be an entity so I can write to chat. But there is no entity function within this file.

How would I go about setting the player to an entity within this file and using it?

Could you please post the resources.lua so we can take a look at what you are talking about.


local YourMoney = 0
local JobNumber = 0
local Waste = 0

function TakeAShit( )
Waste = 0
– EntEmitSound (“Sound/Life/Releif.wav”)
–Print chat here

function IncreaseHumanWaste()
Waste = Waste + 1
SetGlobalInt(“HWaste”, Waste)
timer.Simple(math.Round(math.Rand(4, 13)), IncreaseHumanWaste)

function GetPaid(JobNo)
– EntEmitSound (“Sound/Life/Ching.wav”)
if(JobNo == 1) then
YourMoney = YourMoney + 30
–Print Chat here

if(JobNo == 2) then
YourMoney = YourMoney + 100
–Print Chat here

if(NobNo == 3) then
YourMoney = YourMoney + 125
–Print Chat here

timer.Simple(60, GetPaid(JobNo))


function GetMyMoney( )
– ENT PRINT CHAT You have <YourMoney> in your walet.


player.GetHumans() will return a table containing all the human players. Also you’ll want to store the information about each player on the player object like = 10 and then that 10 will stay with that player in the .money variable. Here’s an example of how that might work:
– Example PayDay function, show setting a vairable using ply.something
timer.Create(“PayDay”, 420, 0, function()
for _, ply in pairs(player.GetHumans()) do
if == nil then = 10
else = + 10

– Example tells all players how much money they have in the chat.

for _, ply in pairs(player.GetHumans() do
ply:ChatPrint(“You have " … tostring( … " dollars in your wallet.”)
Hope that helps.

[editline]17th March 2011[/editline]

So, how would I get 1 player out of the table?

ply variable is one player :wink:
every player have an array this is
but “ply” is an entity

I mean like, if one player had a change in payment, how would I set that one player to the payment value while leaving the rest normal?

Well it depends on how you keep track of things, where do you change the payment at? That would be a good place to already have the info and send it to that player. Maybe you’re not asking the right questions, I have a question how are those functions being called? You could use a think hook and then run though all the players in the think and pass your functions the ply object on at a time. Here’s what your colde would look like doing that:, note: I didn’t test but this is an example.

– Handles Think Time and Pay Time
local doStuffNextThink = CurTime()
local nextPay = CurTime() + 60

– Setup Variables to hold the each players data when they spawn the first time
function PlayerInitialSpawn( ply )
ply.YourMoney = 0
ply.JobNumber = 0
ply.Waste = 0
ply.nextWaste = CurTime() + math.Rand(4,13) – store this with the player so not everybody poops at the same time

hook.Add(“PlayerInitialSpawn”), “initPlayerVars”, PlayerInitalSpawn)

– Think function that makes everything work
function doStuff()

-- Only think every 2 seconds
if !(CurTime() &gt; doStuffNextThink) then return end
doStuffNextThink = CurTime() + 2

-- Let's loop throught the players
for _, ply in pairs(player.GetHumans()) do
	-- Handle Waste
	if CurTime() &gt; ply.nextWaste then
		IncreaseHumanWaste( ply ) -- Call your waste function sending the ply
		ply.nextWaste = CurTime() + math.Rand(4,13) -- set the next time this should be called
	-- Handle Pay
	if CurTime() &gt; nextPay then
		GetPaid( ply )
		ply.nextPay = CurTime() + 60

hook.Add(“Think”, “doStuff”, doStuff)

function IncreaseHumanWaste( ply )
– Increase the waste
ply.Waste = ply.Waste + 1
– You may want to check here to see if it’s over a max level of waste

function GetPaid( ply )

if(	ply.JobNumber == 1) then
	ply.YourMoney = ply.YourMoney + 30
	ply:ChatPrint("You have been paid 30 dollars for your job!")
if(	ply.JobNumber == 2) then
	ply.YourMoney = ply.YourMoney + 100
	ply:ChatPrint("You have been paid 100 dollars for your job!")
if(	ply.JobNumber == 3) then
	ply.YourMoney = ply.YourMoney + 125
	ply:ChatPrint("You have been paid 125 dollars for your job!")


– This is a console command and when run by a player it gets the ply object
– and we tell the ply how much money they have
function GetMyMoney( ply, cmd, args )

ply:ChatPrint("You have " .. tostring(ply.YourMoney) .. " dollars!")

concommand.Add(“show_money”, GetMyMoney)

– Run this and it empties the player waste
function TakeAShit( ply, cmd, args)
ply.Waste = 0
– EntEmitSound (“Sound/Life/Releif.wav”)
ply:ChatPrint(“AHHHHH, that’s better”)
concommand.Add(“take_shit”, TakeAShit)

That works good, thanks.

Oh by the way, that tree script in the previous thread. If I spawn more then 1 it screws up the other’s spawn time. How do I fix?

here’s the tree script edited with comments, the problem was using the local variable to store the time, it was getting reset the next time a new copy of the entity. So what need to happen is the variables need to be stored on the entity it self like this.
include( ‘shared.lua’ )
include( ‘core.lua’ )

–Remove these lines
–local GrowTime = 10 – Only need this once
–local TimerName = “” – save the timer name

function ENT:Initialize()

self.Entity:SetModel( "models/weapons/w_bugbait.mdl" )
-- Set the variables from above like this
self.GrowTime = 10 -- This will stay with just this ent
self.TimerName = "ShowLVLTimer" .. tostring(self)
-- We get self from the function being defined as ENT:function
-- When it's like that it will pass self to the function


function ENT:SpawnFunction( ply, tr )

local SpawnPos = tr.HitPos + tr.HitNormal * 0
local ent = ents.Create( "hm_tree" )
ent:SetPos( SpawnPos )
timer.Create(self.TimerName, 1, 0, ShowLvl, ply, ent )
return ent


– Because this function doesn’t start with ENT: it doesn’t get self
– but ent should be the same entity as self so we can use that because the
– variable go with the entity
function ShowLvl( ply, ent )

ent.GrowTime = ent.GrowTime - 1

if (ent.GrowTime &lt;= 0) then
	self:SetModel( "models/props/CS_militia/tree_large_militia.mdl" )


function ENT:Use( activator, caller )

-- you can use self here becuase the function starts with ENT:
if(self.GrowTime &lt;= 0) then
	SetFoodLvl( activator, 25 )
	if( activator:IsPlayer() ) then
		activator:SetHealth(activator:Health() + 25)
	SafeRemoveEntityDelayed(self, 0)
	if timer.IsTimer(self.TimerName) then timer.Destroy(self.TimerName) end
	if activator:IsPlayer() then activator:ChatPrint("You must first wait for it to grow!") end


Ah yeah, I kind of figured that’d be the problem just wasn’t sure how to write it.

I’ve ran into another slight issue, I’m trying to send data from the client to the server… Do I need to use usermessages for this? Or is there a specific function required to receive data from the client?

ConCommands go from client to server
usermessages go from server to client

What data are you trying to send?

Trying to send the job number from the user’s dialogue to the jobs.lua file on the server. Also the ones you provided return a “unknown command”.

What returned an unknown command? I didn’t think I gave you any commands, I was talking about creating a concommand on the server so you can send info to it like this.

– On the server side of things
function SetJobNum( ply, cmd, args) – ply is the player that sent the command, cmd is the command they used and args is a table of whatever is typed after that
if args[1] == nil then return end – make sure they sent a value after the command
ply.jobnum = tonumber(args[1]) – args is strings so convert it to a number
concommand.Add(“set_job”, SetJobNum)

– on the client side of things whenever you need to send the data
– get’s the value from a fake menu item for the example
RunConsoleCommand(“set_job”, MenuItem.value) – this will run the concommand on the server passing the player that called it, the command that was typed and the args in a table if you wanted more args just keep adding them on the end, and they will be args[2], args[3], etc

Hope that make sense

[editline]19th March 2011[/editline]

if you are talking about the concommand from the earlier code “take_shit” and “show_money”, it looks like they should work, how were you trying to call them?

Just running it through my console, and using the RunConsoleCommand in the script. Either way failed.

hmm, not sure i’ve got mine the same way and they work, i’d say double check the server console for errors, if there was an error while loading the files where the concommands are declared they will not have loaded. Other than that i’m not sure, i’ll post back if I think of something.

Hook ‘doStuff’ Failed: [gamemodes\lifemode\gamemode\jobs.lua:59] attempt to compare nil with number

For some reason the PlayerInitialSpawn hook isn’t working.

But instead, I removed the hook and renamed it to PLS( ply ) then set it to run on the players spawn in GM:InitialSpawn function in the init.lua. And it seems to work… But the way you did it should work but for some reason that initial spawn was screwing up the rest of the script from working.


Sorry for triple posting, but… That doStuff think spams PayDay every 2 seconds everytime it’s first triggered.

Can you please tell me how to fix it?