• Get Player Object or Player Entity?
    14 replies, posted
Ok, even after reading through the wiki and PIL, I still feel like I don't really know what I'm doing. I'm trying to make an RPG using Networked Ints. ([b][url=http://wiki.garrysmod.com/?title=Entity.SetNetworkedInt]Entity.SetNetworkedInt [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b], [b][url=http://wiki.garrysmod.com/?title=Entity.GetNetworkedInt]Entity.GetNetworkedInt [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]) I need to make a function in shared.lua(or included, which is my case) that receives the player's entity/player object so that I can use [b][url=http://wiki.garrysmod.com/?title=Entity.GetNetworkedInt]Entity.GetNetworkedInt [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]. I currently have: [lua] local metaplayer = FindMetaTable("Player") //Get the meta table of player function metaplayer:SetXP(amount) metaplayer:SetNetworkedInt( "xp", amount ) //self:SaveXP() end function metaplayer:GetXP() return metaplayer:GetNetworkedInt( "xp" ) end [/lua] I would be grateful for any help that would point me in the right direction.
This doesn't answer your question, but you have a few typos in your code [code]local metaplayer = FindMetaTable("Player") //Get the meta table of player function metaplayer:SetXP(amount) self:SetNetworkedInt( "xp", amount ) //self:SaveXP() end function metaplayer:GetXP() return self:GetNetworkedInt( "xp" ) end[/code] When in those meta functions (e.g. metaplayer:GetXP), self refers to the player, not metaplayer.
[QUOTE=ceribik;27080883]This doesn't answer your question, but you have a few typos in your code [code]local metaplayer = FindMetaTable("Player") //Get the meta table of player function metaplayer:SetXP(amount) self:SetNetworkedInt( "xp", amount ) //self:SaveXP() end function metaplayer:GetXP() return self:GetNetworkedInt( "xp" ) end[/code] When in those meta functions (e.g. metaplayer:GetXP), self refers to the player, not metaplayer.[/QUOTE] problem with using self: Self does not work in shared.lua [lua] function metaplayer:GetLevel() return self:GetNetworkedInt( "level" ) // line 99 end [/lua] [gamemodes\gmodrpg\gamemode\sh_npc.lua:99] attempt to index local 'self' (a nil value)
That is why you use "if CLIENT then" or "if SERVER then" conditions, or you put each function in a separate file.
so what would i use instead of self for client?
You always use self if you are trying to reference the object/table. The problem is that the function you are trying to call is not available on the client (SetNetworkedInt): this is why you use 2 different functions on the client / server.
what is the function for clients then? SetNWInt?
Think about what you want the client to do, and what the server should do. It might clear things up.
[QUOTE=_nonSENSE;27084649]Think about what you want the client to do, and what the server should do. It might clear things up.[/QUOTE] Well, I just want the server to keep track of the Networked ints, and I'm not sure why the client is trying to call the function, because I never intended them to. hmm, still getting the same result ([gamemodes\gmodrpg\gamemode\sh_npc.lua:125] attempt to index local 'self' (a nil value)), could this be the cause? [lua] function AveragePlayerLvl() if SERVER then local lvl = 0 local count = 0 local total for k, v in pairs(player.GetAll()) do lvl = lvl + v.GetLevel() // this line count = count + 1 end total = lvl/count return total end end function PlayerCount() if SERVER then local count for k, v in pairs(player.GetAll()) do count = count + 1 end return count end end [/lua]
You posted irrelevant code. If you don't want the client to use those functions, why did you try to put them in a shared file. Define Entity:SetXP on the server only. You do not want / need the client to have that. As for the GetXP function, make it return the contents of the NetworkedInt, and then network it to the client. Also, your second function is unnecessary: [lua] local pCount = #player.GetAll() [/lua]
Thanks for that little tidbit of code, saved me from making useless code :) anyway, I guess I can elaborate a little more. Here's my real problem: ERROR: Hook 'NPC_Initialize' Failed: [gamemodes\gmodrpg\gamemode\sh_npc.lua:20] attempt to perform arithmetic on a nil value Removing Hook 'NPC_Initialize' [lua] function NPC_Init() if SERVER then local pCount = #player.GetAll() for _, v in pairs( ents.GetAll() ) do if v:IsNPC() then local health = v:Health() v:SetNetworkedInt("npcxp", ((health*AveragePlayerLvl()) + math.pow(10, (AveragePlayerLvl()/10)))) // this line v:SetHealth((health*AveragePlayerLvl()*pCount)) end end end end hook.Add( "InitPostEntity", "NPC_Initialize", NPC_Init) [/lua] The reason I posted the functions before this post, was because it calls those functions where its saying it is getting a nil value. I made a console cmd version of that function so that I could test to see if it just wasn't setting the NWInt on NPCs that haven't spawned yet. It gives me the error [gamemodes\gmodrpg\gamemode\sh_npc.lua:99] attempt to index local 'self' (a nil value) which is the line I showed you before. [lua] function NPC_ReInit() if SERVER then local pCount = #player.GetAll() for _, v in pairs( ents.GetAll() ) do if v:IsNPC() then if v:GetNetworkedInt("npcxp") == 0 then local health = v:Health() v:SetNetworkedInt("npcxp", ((health*AveragePlayerLvl()) + math.pow(10, (AveragePlayerLvl()/10)))) v:SetHealth((health*AveragePlayerLvl()*pCount)) end end end end end concommand.Add("gmodrpg_reinit_npc", NPC_ReInit) [/lua] I'm trying to figure out what function is returning a nil value in that line. [lua]v:SetNetworkedInt("npcxp", ((health*AveragePlayerLvl()) + math.pow(10, (AveragePlayerLvl()/10)))) // this line[/lua]
AveragePlayerLvl() probably is. v.GetLevel() should be v:GetLevel(). Post that function please.
here. [lua] function metaplayer:GetLevel() if SERVER then return self:GetNetworkedInt( "level" ) end end [/lua] Current Problem: No lua errors, but the NPC won't die, and I can't get hit by the npc. hmmn Finally killed it with a combine ball. console log: [code] ot toybox authentication ] sv_defaultgamemode gmodrpg maxplayers set to 1 Unknown command "sv_unlag_fixstuck" Lua initialized (Lua 5.1) ======== Installing Table (De)Serialiser Module | ver: 1.4 ======== ERROR! Module 'rawio' not found! [addons\npc_control2\lua\autorun\init.lua:2] attempt to call global 'AddCSLua' (a nil value) ======== RD BeamLib v0.23 Installed ======== ========================================================== Simple Prop Protection Version 1.5 by Spacetech has loaded ========================================================== ======================================================= StarGate Pack: Initializing Version: 44 Loading: stargate/shared/init.lua Loading: stargate/shared/bullets.lua Loading: stargate/shared/lib.lua Loading: stargate/shared/matrix.lua Loading: stargate/shared/print_r.lua Loading: stargate/shared/tracelines.lua Loading: stargate/server/init.lua INIParser:parse - File stargate/config.ini successfully parsed INIParser:new - File stargate/user_config.ini does not exist! Loading: stargate/server/entity.lua Loading: stargate/server/ini_parser.lua Loading: stargate/server/protection.lua Loading: stargate/server/spawner.lua Loading: stargate/server/teleport.lua Loading: stargate/server/tool.lua Loading: stargate/server/wire_rd.lua ======================================================= /////////////////////////////// // Ulysses Library // /////////////////////////////// // Loading... // // shared/defines.lua // // server/hook.lua // // server/gamemode_hooks.lua// // shared/misc.lua // // shared/util.lua // // shared/table.lua // // shared/player.lua // // server/player.lua // // shared/messages.lua // // shared/concommand.lua // // server/concommand.lua // // server/util.lua // // shared/sh_ucl.lua // // server/ucl.lua // // server/phys.lua // // server/player_ext.lua // // server/entity_ext.lua // // Load Complete! // /////////////////////////////// [ULIB] Loading SHARED module: ulx_init.lua /////////////////////////////// // ULX Admin Mod // /////////////////////////////// // Loading... // // sh_defines.lua // // lib.lua // // base.lua // // log.lua // // MODULE: chat.lua // // MODULE: fun.lua // // MODULE: menus.lua // // MODULE: rcon.lua // // MODULE: slots.lua // // MODULE: teleport.lua // // MODULE: toolmode.lua // // MODULE: user.lua // // MODULE: util.lua // // MODULE: vote.lua // // MODULE: votemap.lua // // MODULE: userhelp.lua // // end.lua // // Load Complete! // /////////////////////////////// ================================ === Wire 1998 Installed === ================================ ======== Beam NetVars Lib v0.71 Installed ======== ERROR! Module 'zlib_b64' not found! ==== Advanced Duplicator v.1.72 shared module installed! ==== ==== Advanced Duplicator v.1.85 server module installed! ==== Registering gamemode 'fretta' derived from 'base' Registering gamemode 'fretta' derived from 'base' Registering gamemode 'gmodrpg' derived from 'fretta' ScriptEnforce is disabled Couldn't include file 'nuke_vars_init.lua' (File not found) Couldn't include file 'nuke_vars_init.lua' (File not found) [RunString:9825] attempt to call field 'registerDebuggerFormat' (a nil value) [RunString:12595] attempt to call field 'registerDebuggerFormat' (a nil value) [RunString:13156] attempt to call field 'registerDebuggerFormat' (a nil value) Executing listen server config file Couldn't find scene 'scenes/npc/female01/question21.vcd' Couldn't find scene 'scenes/npc/female01/question31.vcd' Couldn't find scene 'scenes/npc/female01/answer35.vcd' Couldn't find scene 'scenes/npc/female01/gordead_ques03a.vcd' Couldn't find scene 'scenes/npc/female01/gordead_ques03b.vcd' Couldn't find scene 'scenes/npc/female01/nice.vcd' Couldn't find scene 'scenes/npc/male01/nice01.vcd' Couldn't find scene 'scenes/npc/male01/nice02.vcd' Couldn't find scene 'scenes/npc/female01/oneforme.vcd' Couldn't find scene 'scenes/npc/Barney/ba_run.vcd' Couldn't find scene 'scenes/npc/Barney/ba_ohshit.vcd' Couldn't find scene 'scenes/npc/Barney/ba_thisisbad.vcd' Couldn't find scene 'scenes/npc/Barney/ba_headcrabs.vcd' Attempted to create unknown entity type gmod_tool! Can't init gmod_tool Attempted to create unknown entity type gmod_tool! Can't init gmod_tool Attempted to create unknown entity type gmod_tool! Can't init gmod_tool Attempted to create unknown entity type gmod_tool! Can't init gmod_tool Attempted to create unknown entity type gmod_tool! Can't init gmod_tool Attempted to create unknown entity type gmod_tool! Can't init gmod_tool Attempted to create unknown entity type gmod_tool! Can't init gmod_tool Attempted to create unknown entity type gmod_tool! Can't init gmod_tool Attempted to create unknown entity type gmod_tool! Can't init gmod_tool Attempted to create unknown entity type gmod_tool! Can't init gmod_tool Couldn't find any entities named ar2_temp, which point_template template4 is specifying. trigger_changelevel to gm_adventure2_beta3 doesn't have a landmarkCan't find landmark ERROR: Hook 'gmod_wire_hologram_shared' Failed: [addons\wire\lua\entities\gmod_wire_hologram\shared.lua:14] attempt to index field 'base_gmodentity' (a nil value) Removing Hook 'gmod_wire_hologram_shared' exec: couldn't exec listenserver.cfg Attemped to precache unknown particle system "blood_impact_blue_01"! ERROR: Hook 'LSTakeDamage' Failed: [addons\life support\lua\autorun\server\sv_lifesupport.lua:396] attempt to compare number with nil Removing Hook 'LSTakeDamage' InitFastCopy: only 51% fast props. Bug? Lua initialized (Lua 5.1) ======== Installing Table (De)Serialiser Module | ver: 1.4 ======== ERROR! Module 'rawio' not found! -NPC Control- Main Content Loaded Episode 2 Content Loaded! -------------------- ======== RD BeamLib v0.23 Installed ======== ========================================================== Simple Prop Protection Version 1.5 by Spacetech has loaded ========================================================== Attemped to precache unknown particle system "blood_impact_blue_01"! Attemped to precache unknown particle system "blood_impact_blue_01"! ======================================================= StarGate Pack: Initializing Version: 44 Loading: stargate/shared/init.lua Loading: stargate/shared/bullets.lua Loading: stargate/shared/lib.lua Loading: stargate/shared/matrix.lua Loading: stargate/shared/print_r.lua Loading: stargate/shared/tracelines.lua Loading: stargate/client/init.lua Loading: stargate/client/menu.lua Loading: stargate/client/modelclipping.lua Loading: stargate/vgui/SAddressPanel.lua Loading: stargate/vgui/SAddressSelect.lua Loading: stargate/vgui/SControlePanel.lua Loading: stargate/vgui/SHelpButton.lua Loading: stargate/vgui/SHTMLHelp.lua ======================================================= RunConsoleCommand blocked - sent before player spawned (ulib_cl_loaded) ======== Beam NetVars Lib v0.71 Installed ======== loading materials loading material: cable/rope_icon loading material: cable/cable2 loading material: cable/xbeam loading material: cable/redlaser loading material: cable/blue_elec loading material: cable/physbeam loading material: cable/hydra loading material: arrowire/arrowire loading material: arrowire/arrowire2 === Loading Wire Model Packs === Loaded: Stargate.txt Loaded: default.txt Loaded: expression2.txt Loaded: cheeze_buttons2.txt Loaded: wire_model_pack_1.txt Loaded: wire_model_pack_1plus.txt Adding Cheeze's Buttons Pack Adding various Buttons from HL2 and Portal Jaanus' Thruster Pack Beer's Model pack --- Missing Vgui material ignore_this_error ERRO
ok, I noticed that npcs that spawn AFTER NPC_Init is called never give any XP. is there a way I can hook when an NPC spawns or entity is created? Because I would prefer not to execute this line of code on every frame...
Sorry, you need to Log In to post a reply to this thread.