• defining variables per player
    4 replies, posted
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 [CODE]function GM:PlayerSpawn( ply ) ply = LocalPlayer() ply.energy = 100 ply.maxenergy = 100 ply.speed = 200 ply.defaulteng = 100 end[/CODE] im getting a load of errors from this, and im using the energy on a hud using ENE = ply.energy Errors: [CODE][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 [/CODE]
:v: Remove "ply = LocalPlayer()" [URL="http://wiki.garrysmod.com/page/Global/LocalPlayer"]LocalPlayer() returns the player object clientside[/URL], 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.
[QUOTE=jackool;47272791]:v: Remove "ply = LocalPlayer()" [URL="http://wiki.garrysmod.com/page/Global/LocalPlayer"]LocalPlayer() returns the player object clientside[/URL], 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.[/QUOTE] ok so your saying this will work? [CODE]function GM:KeyPress(ply, key) if(key == IN_JUMP) then if not(ply:IsOnGround() or ply.DoubleJumped) then if LocalPlayer.energy < 25 then ply:PrintMessage(HUD_PRINTTALK, "You Need More Energy!") else 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)) LocalPlayer.energy = LocalPlayer.energy - 25 end end else if(key == IN_USE) then if (ply:IsOnGround()) then if LocalPlayer.energy < 50 then ply:PrintMessage(HUD_PRINTTALK, "You Need More Energy!") else ply.DoubleJumped = true ply:SetLocalVelocity(Vector(ply:GetVelocity().x, ply:GetVelocity().y, 650)) ply:EmitSound( "sound/sfx/boost.wav" ) LocalPlayer.energy = LocalPlayer.energy - 25 end end end end end[/CODE] because i dont think that looks right [editline]7th March 2015[/editline] And My HUD [CODE]function CoolHUD2() -- Original Name local ENE = LocalPlayer.energy 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 ) ) end end hook.Add( "HUDPaint", "CoolHUD2", CoolHUD2 )[/CODE]
[QUOTE=TheCodingBoss;47272826]ok so your saying this will work? [CODE]function GM:KeyPress(ply, key) if(key == IN_JUMP) then if not(ply:IsOnGround() or ply.DoubleJumped) then if LocalPlayer.energy < 25 then ply:PrintMessage(HUD_PRINTTALK, "You Need More Energy!") else 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)) LocalPlayer.energy = LocalPlayer.energy - 25 end end else if(key == IN_USE) then if (ply:IsOnGround()) then if LocalPlayer.energy < 50 then ply:PrintMessage(HUD_PRINTTALK, "You Need More Energy!") else ply.DoubleJumped = true ply:SetLocalVelocity(Vector(ply:GetVelocity().x, ply:GetVelocity().y, 650)) ply:EmitSound( "sound/sfx/boost.wav" ) LocalPlayer.energy = LocalPlayer.energy - 25 end end end end end[/CODE] because i dont think that looks right [editline]7th March 2015[/editline] And My HUD [CODE]function CoolHUD2() -- Original Name local ENE = LocalPlayer.energy 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 ) ) end end hook.Add( "HUDPaint", "CoolHUD2", CoolHUD2 )[/CODE][/QUOTE] 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 [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/KeyPress]GM/KeyPress[/url] - 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 [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/SetNWInt]Entity:SetNWInt[/url] 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.
[QUOTE=jackool;47272892]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 [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/KeyPress]GM/KeyPress[/url] - 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 [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/SetNWInt]Entity:SetNWInt[/url] 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.[/QUOTE] Thanks :)
Sorry, you need to Log In to post a reply to this thread.