• Trying to learn Lua, simple kill counter issue.
    13 replies, posted
Hello everyone, so I'm trying to learn lua (experienced in java) and I'm curious as to why this wont work. It displays on the hud, but wont increase the number. init.lua: [lua]//Simple Kill Counter AddCSLuaFile( "cl_init.lua" ) function KillCounter( victim, killer, weapon ) --Sets up a new function called KillCounter if killer:GetNWInt("killcounter") == 50 then --If the killcounter variable equals 50 then do something PrintMessage(HUD_PRINTTALK, "Player" .. killer:GetName() .. "Has Won") --When the killcounter equals 50 it will print this "Player <playername> has won timer.Simple(3, function() --Sets up a timer for three seconds game.ConsoleCommand("changelevel " ..table.Random(maps).. "\n") --When the timer finishes it excecutes this console command local maps = {"gm_construct", "gm_flatgrass", "cs_office", "cs_italy" } end) end killer:SetNWInt("killcounter", killer:GetNWInt("killcounter") + 1) --Adds 1 everytime an NPC is killed. hook.Add("OnNPCKilled","KillCounter", KillCounter) [/lua] cl_init.lua: [lua]function killcounter() draw.WordBox( 8, ScrW() - 920, ScrH() - 98, "KillCount: "..LocalPlayer():GetNWInt("killcounter"),"ScoreboardText",Color(200,0,0,0),Color(255,255,255,255)) end hook.Add("HUDPaint","KillCounter",killcounter) [/lua] And both my files are located at: addons/KillCounter/lua/autorun Sorry if I'm doing something terribly wrong, I'm still trying to learn.
Well, for one, don't use NWInt's, because you are new let's try to not get you on a bad habit. Now, if you consider what I said I'll help you in the process of sending the amount to the client in a much more effective, working way.
I think you have to do if server and if client checks, you said the files are in lua/autorun. [lua] game.ConsoleCommand("changelevel " ..table.Random(maps).. "\n") local maps = {"gm_construct", "gm_flatgrass", "cs_office", "cs_italy" } [/lua] To me it seems your using game.ConsoleCommand on a nil table, since it doesn't exist when your using it. edit: Use usermessages. [lua] //Put this code in the npc killed hook killer.Killz=(killer.Killz or 0)+1 usmg.Start("IncreaseKills",killer) umsg.Float(killer.Killz) umsg.End() [/lua] client [lua] usermessage.Hook("IncreaseKills",function(um) myKills=um:ReadFloat() end) //Have the code draw myKills instead of the NWInt [/lua]
So how would that look, where would the if server and if client checks go? Would it still be in two files, or would it now be in one? Also, @jrj996; I'm open for any suggestions you have for me.
Clientside files go in lua/autorun/client, if it's in a shared, you do [lua] if CLIENT then /*code*/ end [/lua]
So, would it look like this: init: [lua]//Simple Kill Counter AddCSLuaFile( "cl_init.lua" ) function KillCounter( victim, killer, weapon ) --Sets up a new function called KillCounter if killer:GetNWInt("killcounter") == 50 then --If the killcounter variable equals 50 then do something PrintMessage(HUD_PRINTTALK, "Player" .. killer:GetName() .. "Has Won") --When the killcounter equals 50 it will print this "Player <playername> has won timer.Simple(3, function() --Sets up a timer for three seconds game.ConsoleCommand("changelevel " ..table.Random(maps).. "\n") --When the timer finishes it excecutes this console command local maps = {"gm_construct", "gm_flatgrass", "cs_office", "cs_italy" } end) end killer.Killz=(killer.Killz or 0)+1 usmg.Start("IncreaseKills",killer) umsg.Float(killer.Killz) umsg.End() hook.Add("OnNPCKilled","KillCounter", KillCounter) [/lua] cl_init(I'm not sure how I would refer to a the correct value here): [lua]usermessage.Hook("IncreaseKills",function(um) myKills=um:ReadFloat() end) function killcounter() draw.WordBox( 8, ScrW() - 920, ScrH() - 98, "KillCount: "..LocalPlayer():GetNWInt("killcounter"),"ScoreboardText",Color(200,0,0,0),Color(255,255,255,255)) end hook.Add("HUDPaint","KillCounter",killcounter) [/lua] Also, I moved both files to the location you specified. Would this work? It doesn't seem right to me.
[QUOTE=Brandan;31180032] client [lua] usermessage.Hook("IncreaseKills",function(um) myKills=um:ReadFloat() end) //Have the code draw myKills instead of the NWInt <<<< [/lua][/QUOTE] [editline]17th July 2011[/editline] [lua]//Simple Kill Counter if SERVER then AddCSLuaFile( "cl_init.lua" ) end function KillCounter( victim, killer, weapon ) --Sets up a new function called KillCounter if killer:GetNWInt("killcounter") == 50 then --If the killcounter variable equals 50 then do something PrintMessage(HUD_PRINTTALK, "Player" .. killer:GetName() .. "Has Won") --When the killcounter equals 50 it will print this "Player <playername> has won timer.Simple(3, function() --Sets up a timer for three seconds local maps = {"gm_construct", "gm_flatgrass", "cs_office", "cs_italy" } game.ConsoleCommand("changelevel " ..table.Random(maps).. "\n") --When the timer finishes it excecutes this console command end) end killer.Killz=(killer.Killz or 0)+1 usmg.Start("IncreaseKills",killer) umsg.Float(killer.Killz) umsg.End() hook.Add("OnNPCKilled","KillCounter", KillCounter) [/lua] [lua] if CLIENT then usermessage.Hook("IncreaseKills",function(um) myKills=um:ReadFloat() end) function killcounter() draw.WordBox( 8, ScrW() - 920, ScrH() - 98, "KillCount: "..myKills,"ScoreboardText",Color(200,0,0,0),Color(255,255,255,255)) end hook.Add("HUDPaint","KillCounter",killcounter) end [/lua] If your files are in lua/autorun/*optional folder here* then don't name them init.lua and cl_init.lua, name them something else. To make things easier put clientside code in a new lua file in lua/autorun/client
I have a post from c-unit below. Please, don't take advice from brandon, he is a noob. [lua] if( SERVER ) then AddCSLuaFile("this_files_name.lua"); local maps = {"gm_construct", "gm_flatgrass", "cs_office", "cs_italy" } hook.Add("OnNPCKilled", "KillCounter", function(victim, killer, weapon) killer.Killz = killer.Killz + 1 || 1 umsg.Start("IncreaseKills", killer) umsg.Float( killer.Killz ); umsg.End(); if( killer.Killz >= 50 ) then PrintMessage(HUD_PRINTTALK, "Player " .. killer:GetName() .. " has won! Map changing in 5 seconds!") timer.Simple(5, function() game.ConsoleCommand("changelevel " ..table.Random(maps).. "\n") end) end end) else local pKillz; usermessage.Hook("IncreaseKillz", function( um ) pKillz = um:ReadFloat() end) hook.Add("HUDPaint", "drawkills", function() draw.SimpleTextOutlined("Kills: "..pKillz, "TargetID", 15, 15, Color(0, 0, 0), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 5, Color(255, 0, 0)) end) end[/lua]
Thanks for the help guys, it means a lot. I'm able to understand most of it, I was just curious as to why I should/need to in-case some of the code in an "if (server)" statement. Because wouldn't that mean if gmod wasn't running on a server then it wont run the first part of the code? Isn't it like saying; "if server is true then run this code: if server is false else run this code:"?
There are three Lua states in GMod. Menu, client and server. You don't want the OnNPCKilled hook to run on the client, as it doesn't work on the client. It only works on the server.
Ah, I get it. It basically is making that code run on the server per-say. I get this error when trying ever I kill an NPC, Hook 'KillCounter' Failed: [addons\killcounter\lua\autorun\killcounter.lua:9] attempt to perform arithmetic on field 'Killz' (a nil value) Is that saying that because I didn't define the variable before or something of that sort?
---- [editline]19th July 2011[/editline] [lua]if( SERVER ) then AddCSLuaFile("this_files_name.lua"); hook.Add("PlayerInitialSpawn", "KillCounter", function(ply) ply.Killz = ply.Killz or 1 end) local maps = {"gm_construct", "gm_flatgrass", "cs_office", "cs_italy" } hook.Add("OnNPCKilled", "KillCounter", function(victim, killer, weapon) killer.Killz = killer.Killz + 1 || 1 umsg.Start("IncreaseKills", killer) umsg.Float( killer.Killz ); umsg.End(); if( killer.Killz >= 50 ) then PrintMessage(HUD_PRINTTALK, "Player " .. killer:GetName() .. " has won! Map changing in 5 seconds!") timer.Simple(5, function() game.ConsoleCommand("changelevel " ..table.Random(maps).. "\n") end) end end) else local pKillz; usermessage.Hook("IncreaseKillz", function( um ) pKillz = um:ReadFloat() end) hook.Add("HUDPaint", "drawkills", function() draw.SimpleTextOutlined("Kills: "..pKillz, "TargetID", 15, 15, Color(0, 0, 0), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 5, Color(255, 0, 0)) end) end[/lua] [editline]19th July 2011[/editline] Also make sure the killer is an player or you'll have errors in your gamemode later on trying to add an kill to the npc or vehicle. [editline]19th July 2011[/editline] Also make sure the killer is an player or you'll have errors in your gamemode later on trying to add an kill to the npc or vehicle. [editline]19th July 2011[/editline] Also make sure the killer is an player or you'll have errors in your gamemode later on trying to add an kill to the npc or vehicle.
[QUOTE=FlapadarV2;31183316]I have a post from c-unit below. Please, don't take advice from brandon, he is a noob. [lua] if( SERVER ) then AddCSLuaFile("this_files_name.lua"); local maps = {"gm_construct", "gm_flatgrass", "cs_office", "cs_italy" } hook.Add("OnNPCKilled", "KillCounter", function(victim, killer, weapon) killer.Killz = killer.Killz + 1 || 1 umsg.Start("IncreaseKills", killer) umsg.Float( killer.Killz ); umsg.End(); if( killer.Killz >= 50 ) then PrintMessage(HUD_PRINTTALK, "Player " .. killer:GetName() .. " has won! Map changing in 5 seconds!") timer.Simple(5, function() game.ConsoleCommand("changelevel " ..table.Random(maps).. "\n") end) end end) else local pKillz; usermessage.Hook("IncreaseKillz", function( um ) pKillz = um:ReadFloat() end) hook.Add("HUDPaint", "drawkills", function() draw.SimpleTextOutlined("Kills: "..pKillz, "TargetID", 15, 15, Color(0, 0, 0), TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT, 5, Color(255, 0, 0)) end) end[/lua][/QUOTE] Is there any reason you felt the need to send all the code to the client? Why not keep the client and server files separated?
[QUOTE=thegrb93;31203271]Is there any reason you felt the need to send all the code to the client? Why not keep the client and server files separated?[/QUOTE] Not my code; c-units. Though there's no real benefit of splitting files unless you want to hide exploitable code.
Sorry, you need to Log In to post a reply to this thread.