I'm trying to create a kill counter for a TDM of players. Every example I see on Google only has NPC kill counter. Could anybody help me with me?
[QUOTE=ErraticFox;43641149]I'm trying to create a kill counter for a TDM of players. Every example I see on Google only has NPC kill counter. Could anybody help me with me?[/QUOTE]
You should edit NPC kill counter code to make it count players' deaths.
[CODE]
local Kills = 0
hook.Add( "PlayerDeath", "player_death", function( ply, weapon, attacker )
if ( ply != attacker ) then
attacker.Kills = attacker.Kills + 1
end
end
[/CODE]
Yes, no, maybe?
I'm not exactly sure what you want, but I assume this?
[QUOTE=I'm great;43641210]You should edit NPC kill counter code to make it count players' deaths.[/QUOTE]
I'll expand on what I'm great said.
To do what he's saying you will have to use the correct hook. I'll help you out and tell you the hook you want to use is [URL="http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/indexbdc5.html"]"PlayerDeath"[/URL]
So, for this [URL="http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index2b0f.html"]tutorial[/URL], you could basically change the:
[lua]
hook.Add("OnNPCKilled","KillCounter", KillCounter)
[/lua]
with
[lua]
hook.Add("PlayerDeath","KillCounter", KillCounter)
[/lua]
[QUOTE=Alig96;43641273]-Helpful post-
[/QUOTE]
Yes, but you also would need to add a check to see if they didn't kill themselves
[QUOTE=AnonTakesOver;43641300]Yes, but you also would need to add a check to see if they didn't kill themselves[/QUOTE]
If your going to be like that about it, you'd have to check, if they're actually a player as-well (e.g if the world kills you, the world would get an increase on the counter). There's a lot of other validation you could do, but I only said the core of what he needed.
[QUOTE=Alig96;43641342]If your going to be like that about it, you'd have to check, if they're actually a player as-well (e.g if the world kills you, the world would get an increase on the counter). There's a lot of other validation you could do, but I only said the core of what he needed.[/QUOTE]
Very true,
I assume OP has got what he has needed and can mark this as solved unless he has any further questions.
All I basically want is a killstreak. Like Call of Duty. You kill 3 people, you get a 3 killstreak. I will be playing a sound for specific killstreak kills though.
[QUOTE=ErraticFox;43642084]All I basically want is a killstreak. Like Call of Duty. You kill 3 people, you get a 3 killstreak. I will be playing a sound for specific killstreak kills though.[/QUOTE]
[CODE]
local Kills = 0
hook.Add( "PlayerDeath", "player_death", function( ply, weapon, attacker )
if ( ply != attacker ) then
attacker.Kills = attacker.Kills + 1
if ( attacker.Kills == 3 ) then
-- pew pew UAV inbound bang bang bang
-- Insert stuff here
end
end
ply.Kills = 0 -- Resets the players killstreak if they die
end
[/CODE]
And for playing sounds for only the player getting the killstreak look here:
[url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index69db.html[/url]
[QUOTE=AnonTakesOver;43642092][CODE]
local Kills = 0
hook.Add( "PlayerDeath", "player_death", function( ply, weapon, attacker )
if ( ply != attacker ) then
attacker.Kills = attacker.Kills + 1
if ( attacker.Kills == 3 ) then
-- pew pew UAV inbound bang bang bang
-- Insert stuff here
end
end
ply.Kills = 0 -- Resets the players killstreak if they die
end
[/CODE]
And for playing sounds for only the player getting the killstreak look here:
[url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index69db.html[/url][/QUOTE]
Okay, my last question, just because I have no one to try it with; How would I incorporate it also counting NPC kills?
[QUOTE=ErraticFox;43642177]Okay, my last question, just because I have no one to try it with; How would I incorporate it also counting NPC kills?[/QUOTE]
[CODE]
local Kills = 0
hook.Add( "PlayerDeath", "player_death", function( ply, weapon, attacker )
GiveKills(ply, attacker)
end
hook.Add( "OnNPCKilled", "on_npc_killed", function( npc, killer, weapon )
GiveKills(npc, killer)
end
function GiveKills(ply, attacker)
if ( ply != attacker and attacker:IsPlayer()) then
attacker.Kills = attacker.Kills + 1
if ( attacker.Kills == 3 ) then
-- pew pew UAV inbound bang bang bang
-- Insert stuff here
end
end
if( ply:IsPlayer() ) then
ply.Kills = 0 -- Resets the players killstreak if they die
end
end
[/CODE]
Untested, apologize for any horrible spacing, I don't mean to do it, it's the way the forum is reading my tab spaces
Alright, now I get a error on line 9 "near 'hook'" which is [CODE]hook.Add( "PlayerDeath", "player_death", function( ply, weapon, attacker )[/CODE]
It looks fine to me? :\
[QUOTE=ErraticFox;43642491]Alright, now I get a error on line 9 "near 'hook'" which is [CODE]hook.Add( "PlayerDeath", "player_death", function( ply, weapon, attacker )[/CODE]
It looks fine to me? :\[/QUOTE]
What the error says? Copy and paste it here with the entire source code.
This is my init.lua, hopefully I'm doing this right. I'm trying to learn LUA.
[IMG]http://puu.sh/6vfFw.png[/IMG]
[CODE]AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
include("player.lua")
local Kills = 0
hook.Add( "PlayerDeath", "player_death", function( ply, weapon, attacker )
GiveKills(ply, attacker)
end
hook.Add( "OnNPCKilled", "on_npc_killed", function( npc, killer, weapon )
GiveKills(npc, killer)
end
function GiveKills(ply, attacker)
if ( ply != attacker and attacker:IsPlayer()) then
attacker.Kills = attacker.Kills + 1
if ( attacker.Kills == 3 ) then
-- pew pew UAV inbound bang bang bang
-- Insert stuff here
end
end
if( ply:IsPlayer() ) then
ply.Kills = 0 -- Resets the players killstreak if they die
end
end
function GM:PlayerConnect( name, ip )
print("Player: " .. name .. ", has joined the game.")
end
function GM:PlayerInitialSpawn( ply )
print("Player: " .. ply:Nick() .. ", has spawned.")
ply:SetGravity( 1 )
ply:SetWalkSpeed( 600 )
ply:SetRunSpeed( 900 )
ply:SetGamemodeTeam( 0 )
end
function GM:PlayerSpawn (ply)
ply:Give( "weapon_Pistol" )
end
function GM:PlayerAuthed( ply, steamID, uniqueID )
print("Player: " .. ply:Nick() .. ", has gotten authed.")
end[/CODE]
[CODE]hook.Add( "PlayerDeath", "player_death", function( ply, weapon, attacker )
GiveKills(ply, attacker)
end)
hook.Add( "OnNPCKilled", "on_npc_killed", function( npc, killer, weapon )
GiveKills(npc, killer)
end)[/CODE]
You forgot the brackets at the end.
When I killed an NPC, I get this error... Why is this?
[IMG]http://puu.sh/6vh8l.png[/IMG]
Because the attackers kills are nil and you are trying to increase them. There is no initial value for their kills. Use a spawn hook to set their kills to 0 and it won't error.
[QUOTE=Linda;43642986]Because the attackers kills are nil and you are trying to increase them. There is no initial value for their kills. Use a spawn hook to set their kills to 0 and it won't error.[/QUOTE]
How would I do that? I thought the
function GiveKills(ply, attacker)
was for that...
ply.Kills = 0
In the spawn hook.
Like this?
[CODE]hook.Add( "PlayerSpawnNPC", "playerSpawnNPCTest", NPCSpawn function()
ply.Kills = 0
end)
[/CODE]
I still get an error obviously because of function, but I don't know what to do past that part. I don't even know if I it right.
No...The PlayerSpawn hook. The one that is already in your code.
This?... If so still error nil.
Btw, sorry if I'm being a complete nub right now. This is my first ever lua 'project'. More then anything, my first lua anything. :\
function GM:PlayerSpawn (ply)
ply.Kills = 0
ply:Give( "weapon_Pistol" )
end
Post the line of code it errors on, along with the full error. I can't see the whole Page on my phone.
[IMG]http://puu.sh/6vjWV.png[/IMG]
[CODE]--[[---------------------------------------------------------
Sandbox Gamemode
This is GMod's default gamemode
-----------------------------------------------------------]]
-- These files get sent to the client
AddCSLuaFile( "cl_hints.lua" )
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "cl_notice.lua" )
AddCSLuaFile( "cl_search_models.lua" )
AddCSLuaFile( "cl_spawnmenu.lua" )
AddCSLuaFile( "cl_worldtips.lua" )
AddCSLuaFile( "persistence.lua" )
AddCSLuaFile( "player_extension.lua" )
AddCSLuaFile( "save_load.lua" )
AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "gui/IconEditor.lua" )
include( 'shared.lua' )
include( 'commands.lua' )
include( 'player.lua' )
include( 'spawnmenu/init.lua' )
--
-- Make BaseClass available
--
DEFINE_BASECLASS( "gamemode_base" )
--[[---------------------------------------------------------
Name: gamemode:PlayerSpawn( )
Desc: Called when a player spawns
-----------------------------------------------------------]]
local Kills = 0
hook.Add( "PlayerDeath", "player_death", function( ply, weapon, attacker )
GiveKills(ply, attacker)
end)
hook.Add( "OnNPCKilled", "on_npc_killed", function( npc, killer, weapon )
GiveKills(npc, killer)
end)
function GiveKills(ply, attacker)
if ( ply != attacker and attacker:IsPlayer()) then
attacker.Kills = attacker.Kills + 1
if ( attacker.Kills == 3 ) then
PrintMessage(HUD_PRINTTALK, "Player" .. killer:GetName() .. "Has Won")
end
end
if( ply:IsPlayer() ) then
ply.Kills = 0 -- Resets the players killstreak if they die
end
end
function GM:PlayerConnect( name, ip )
print("Player: " .. name .. ", has joined the game.")
end
function GM:PlayerInitialSpawn( ply )
print("Player: " .. ply:Nick() .. ", has spawned.")
ply:SetGravity( 1 )
ply:SetWalkSpeed( 600 )
ply:SetRunSpeed( 900 )
ply:SetGamemodeTeam( 0 )
end
function GM:PlayerSpawn (ply)
ply.Kills = 0
ply:Give( "weapon_Pistol" )
end
function GM:PlayerAuthed( ply, steamID, uniqueID )
print("Player: " .. ply:Nick() .. ", has gotten authed.")
end
function GM:PlayerSpawn( pl )
player_manager.SetPlayerClass( pl, "player_sandbox" )
BaseClass.PlayerSpawn( self, pl )
end
--[[---------------------------------------------------------
Name: gamemode:OnPhysgunFreeze( weapon, phys, ent, player )
Desc: The physgun wants to freeze a prop
-----------------------------------------------------------]]
function GM:OnPhysgunFreeze( weapon, phys, ent, ply )
-- Don't freeze persistent props (should already be froze)
if ( ent:GetPersistent() ) then return false end
BaseClass.OnPhysgunFreeze( self, weapon, phys, ent, ply )
ply:SendHint( "PhysgunUnfreeze", 0.3 )
ply:SuppressHint( "PhysgunFreeze" )
end
--[[---------------------------------------------------------
Name: gamemode:OnPhysgunReload( weapon, player )
Desc: The physgun wants to unfreeze
-----------------------------------------------------------]]
function GM:OnPhysgunReload( weapon, ply )
local num = ply:PhysgunUnfreeze()
if ( num > 0 ) then
ply:SendLua( "GAMEMODE:UnfrozeObjects("..num..")" )
end
ply:SuppressHint( "PhysgunReload" )
end
--[[---------------------------------------------------------
Name: gamemode:PlayerShouldTakeDamage
Return true if this player should take damage from this attacker
Note: This is a shared function - the client will think they can
damage the players even though they can't. This just means the
prediction will show blood.
-----------------------------------------------------------]]
function GM:PlayerShouldTakeDamage( ply, attacker )
-- The player should always take damage in single player..
if ( game.SinglePlayer() ) then return true end
-- Global godmode, players can't be damaged in any way
if ( cvars.Bool( "sbox_godmode", false ) ) then return false end
-- No player vs player damage
if ( attacker:IsValid() && attacker:IsPlayer() ) then
return cvars.Bool( "sbox_playershurtplayers", true )
end
-- Default, let the player be hurt
return true
end
--[[---------------------------------------------------------
Show the search when f1 is pressed
-----------------------------------------------------------]]
function GM:ShowHelp( ply )
ply:SendLua( "hook.Run( 'StartSearch' )" );
end
--[[---------------------------------------------------------
Called once on the player's first spawn
-----------------------------------------------------------]]
function GM:PlayerInitialSpawn( ply )
BaseClass.PlayerInitialSpawn( self, ply )
end
--[[---------------------------------------------------------
Desc: A ragdoll of an entity has been created
-----------------------------------------------------------]]
function GM:CreateEntityRagdoll( entity, ragdoll )
-- Replace the entity with the ragdoll in cleanups etc
undo.ReplaceEntity( entity, ragdoll )
cleanup.ReplaceEntity( entity, ragdoll )
end
--[[---------------------------------------------------------
Name: gamemode:PlayerUnfrozeObject( )
-----------------------------------------------------------]]
function GM:PlayerUnfrozeObject( ply, entity, physobject )
local effectdata = EffectData()
effectdata:SetOrigin( physobject:GetPos() )
effectdata:SetEntity( entity )
util.Effect( "phys_unfreeze", effectdata, true, true )
end
--[[---------------------------------------------------------
Name: gamemode:PlayerFrozeObject( )
-----------------------------------------------------------]]
function GM:PlayerFrozeObject( ply, entity, physobject )
if ( DisablePropCreateEffect ) then return end
local effectdata = EffectData()
effectdata:SetOrigin( physobject:GetPos() )
effectdata:SetEntity( entity )
util.Effect( "phys_freeze", effectdata, true, true )
end
--
-- Who can edit variables?
-- If you're writing prop protection or something, you'll
-- probably want to hook or override this function.
--
function GM:CanEditVariable( ent, ply, key, val, editor )
-- Only allow admins to edit admin only variables!
if ( editor.AdminOnly ) then
return ply:IsAdmin()
end
-- This entity decides who can edit its variables
if ( isfunction( ent.CanEditVariables ) ) then
return ent:CanEditVariables( ply )
end
-- default in sandbox is.. anyone can edit anything.
return true
end[/CODE]
[lua]function GM:PlayerSpawn (ply)
ply.Kills = 0
ply:Give( "weapon_Pistol" )
end
function GM:PlayerSpawn( pl )
player_manager.SetPlayerClass( pl, "player_sandbox" )
BaseClass.PlayerSpawn( self, pl )
end
[/lua]
Don't do that. Merge them all into one function. You have two PlayerSpawn functions.
[lua]function GM:PlayerSpawn(ply)
player_manager.SetPlayerClass( ply, "player_sandbox" )
BaseClass.PlayerSpawn( self, ply )
ply.Kills = 0
ply:Give( "weapon_Pistol" )
end[/lua]
Edit: wait, are you editing sandbox or making your own gamemode? I hope you're not editing sandbox...
When I get three kills or the killstreak I get this error:
[IMG]http://puu.sh/6vlnx.png[/IMG]
Thanks for all the help though. Is there anyway I'd be able to separate this into a separate lua file?
It should be attacker:GetName()
This gives me the same error, but with index global 'surface' <a nil value> :\
[CODE]function GiveKills(ply, attacker)
if ( ply != attacker and attacker:IsPlayer()) then
attacker.Kills = attacker.Kills + 1
if ( attacker.Kills == 3 ) then
surface.PlaySound( "/music/quake/monsterkill.wav" )
end
end
if( ply:IsPlayer() ) then
ply.Kills = 0 -- Resets the players killstreak if they die
end
end[/CODE]
You're trying to run surface.PlaySound() on the server.
[lua]
function GiveKills(ply, attacker)
if ( ply != attacker and attacker:IsPlayer()) then
attacker.Kills = attacker.Kills + 1
if ( attacker.Kills == 3 ) then
BroadcastLua("surface.PlaySound( '/music/quake/monsterkill.wav' )")
end
end
if( ply:IsPlayer() ) then
ply.Kills = 0 -- Resets the players killstreak if they die
end
end
[/lua]
Now I get missing "Failed to load sound "\music\quake\monsterkill.wav", file probably missing from disk/repository" and I was checking up on this error and it said maybe cause of bit rate? I also noticed it changed the backslashes to forward slashes.
Sorry, you need to Log In to post a reply to this thread.