defining variables per player

Hi! im working on a superjump script with energy, unfortunatly its not going to well,
im using variables defined to the player but im not sure if this is the right way

My code

function GM:PlayerSpawn( ply )
	ply = LocalPlayer() = 100
	ply.maxenergy = 100
	ply.speed = 200
	ply.defaulteng = 100

im getting a load of errors from this, and im using the energy on a hud using ENE =


[ERROR] gamemodes/advw/gamemode/cl_init.lua:103: attempt to concatenate local 'ENE' (a nil value) -- coming from my hud
  1. fn - gamemodes/advw/gamemode/cl_init.lua:103
   2. unknown - addons/ulib/lua/ulib/shared/hook.lua:184


Remove “ply = LocalPlayer()”

LocalPlayer() returns the player object clientside, and if this is on the server then this won’t work at all.

Plus you don’t need to redefine the player object for no reason. There is no reason to set “ply” to anything.

You use LocalPlayer() for stuff like the clientside HUD, and usually you won’t ever want to set LocalPlayer() to a variable unless you have a really, really good reason.

ok so your saying this will work?

function GM:KeyPress(ply, key)
	if(key == IN_JUMP) then
		if not(ply:IsOnGround() or ply.DoubleJumped) then
			if < 25 then
				ply:PrintMessage(HUD_PRINTTALK, "You Need More Energy!")
			ply:EmitSound( "sound/sfx/boost.wav" )
			ply.DoubleJumped = true
			ply:EmitSound( "sound/sfx/boost.wav" )
			ply:SetLocalVelocity(Vector(ply:GetVelocity().x, ply:GetVelocity().y, 400)) = - 25
		if(key == IN_USE) then
		if (ply:IsOnGround()) then
		if < 50 then
				ply:PrintMessage(HUD_PRINTTALK, "You Need More Energy!")
			ply.DoubleJumped = true
			ply:SetLocalVelocity(Vector(ply:GetVelocity().x, ply:GetVelocity().y, 650))
			ply:EmitSound( "sound/sfx/boost.wav" ) = - 25

because i dont think that looks right

[editline]7th March 2015[/editline]

And My HUD

function CoolHUD2() -- Original Name

	local ENE =
	local ARM = ply:Armor()
	if ENE == 0 then
	-- Do Nothing
	elseif ENE == ENE then
	draw.WordBox( 4, 30, ScrH() - 175, "Energy", "ChatFont", Color( 255, 255, 255, 255 ), Color ( 150, 200, 0, 255 ) )
	draw.RoundedBox( 4, 30, ScrH() - 150, 200, 15, Color( 0, 0, 0, 60 ) )
	draw.RoundedBox( 4, 30, ScrH() - 150, math.Clamp( ENE, 0, 200)*2, 15, Color( 150, 200, 0, 150 ) )
	draw.RoundedBox( 4, 30, ScrH() - 150, math.Clamp( ENE, 0, 200)*2, 15, Color( 150, 200, 0, 40 ) )
hook.Add( "HUDPaint", "CoolHUD2", CoolHUD2 )

There are… a lot of issues here. First, you are using LocalPlayer wrong - it requires brackets () because it is a function.

Second, you don’t even need to use LocalPlayer() in that case - because the player object is supplied to you in

GM/KeyPress - so in the KeyPress part of it you should be using ply.

Third, this isn’t going to work correctly clientside. Setting the player’s velocity isn’t going to work clientside.

Fourth, this is going to require some networking because you want the variables to be on both the server and the client.
For ease of use, look into NWVars, specifically

Entity:SetNWInt in your case.

Fifth, you are going to need to set the jump power of the player or at least make them unable to jump normally for this to work correctly. This is kind of a weird way of doing things.

Someone can probably better explain this to you (or give you some example code), because my explanations are probably a little more complex for someone new to programming.

Thanks :slight_smile: