errors with my script

Hi!
im working on an energy script for one of my upcoming gamemodes but for some reason this code throws millions of errors at me, i cant seem to find the problem, if anyone else can help that would be superb

Errors:


[ERROR] gamemodes/advw/gamemode/cl_init.lua:118: attempt to index global 'play' (a nil value)
  1. fn - gamemodes/advw/gamemode/cl_init.lua:118
   2. unknown - addons/ulib/lua/ulib/shared/hook.lua:184


My Code:


function setenergy ( ply )
play.energy = 30 -- my global variable
end

local speed = 200

local goal = 30

 function energyregain ( ply )
        if play.energy == 30 then
        ply.MsgN("No Energy Regain Needed")
else
	variable = math.Approach( play.energy, goal, speed * FrameTime() )
	end
end

Take a look at my simple currency system ( only missing read / write functions ):
https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_currency_system/sv_currency_system.lua.html
https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_currency_system/_sh_currency_system.lua.html
https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_currency_system/cl_currency_system.lua.html

Or zipped:
https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/simple_currency_system/simple_currency_system.zip

And, also take a look at my stamina system: https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/basic_stamina_system/sh_basic_stamina_system.lua.html

and extended: https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/basic_stamina_system/sh_extended_stamina_system.lua.html

If you want the player to have a variable associated with them, assign it to the player object ( the reference in your case would be ply ) instead of as a global. You end up making more work for yourself when you use globals unless it is a storage var and you’re able to access the data efficiently / easily.

Hopefully, by looking at how the Stamina / Currency systems are done, it’ll help you with your energy system. For Stamina, there are a few instances where Stamina gets “reset” and those times it is networked / networked to reset. Otherwise, things are handled in a shared SetupMove hook so that stamina will always be the same on client and server. Even recovering stamina will be linked without needing to network it.

Hopefully these help.

Acecool, that is not helpful in the slightest.

Make sure “play” is declared on the Client because global variables cannot transfer from client to server without networking. The error literally means that “play” is nil and without seeing the entirety of the code, we cant help much.

Youve meant to use ply.energy, not play.energy, you should really consider looking through your code before posting a ohmygod pls pls pls help me fixing my code thread (or at least use problems which dont need their own thread thread) :vee: