• making it so after number of kills you get another gun
    22 replies, posted
I remember playing a gamemode a long time ago in gmod 9 where if you get a certain number of player kills you get a better gun, i only understand basic lua right now and was wondering if anyone could tell me how to do it?
- You would have to keep track of the current amount of damage done for a player, you would do this in a variable. - You will have to add the 'Dealt Damage' to the player's "Damage done" variable, you could do this in the OnEntityTakeDamage hook for example. - After adding this damage, you can check if it's enough for the next "weapon-tier". If so, you could give the player his/her new damage and, if preferred, reset the damage, maybe keep track of a "current weapon-rank" as well. A quick example that should get you started: [code] -- Serverside function GM:OnPlayerSpawn( ply ) ply.currentDealtDamage = 0; end function GM:OnEntityTakeDamage( ent, inflictor, attacker, amount, dmginfo ) -- If we're dealing damage to ourself, abort. Remove if you want Player vs Player damage. if ( ent == attacker || ( ent:IsPlayer() && attacker:IsPlayer() ) ) then return end -- Register if damage dealt by a Player to a valid target if ( attacker:IsPlayer() ) then -- add && ent:IsNPC() or other checks if you want the damage to only work with certain entities. Without this, you would also get damage from props, etc. ply.currentDealtDamage = ply.currentDealtDamage + dmginfo:GetDamage(); if (ply.currentDealtDamage > 9001) then -- give weapon code, etc. end end end [/code] Good luck!
[QUOTE=Claiver;35599332]- You would have to keep track of the current amount of damage done for a player, you would do this in a variable. - You will have to add the 'Dealt Damage' to the player's "Damage done" variable, you could do this in the OnEntityTakeDamage hook for example. - After adding this damage, you can check if it's enough for the next "weapon-tier". If so, you could give the player his/her new damage and, if preferred, reset the damage, maybe keep track of a "current weapon-rank" as well. A quick example that should get you started: Good luck![/QUOTE] I think he was looking for something like this: [lua] local Reset_On_Death = true -- Set to false if you don't want the player's amount of kills to be reset when they die. local WeaponsTable = {} -- WeaponsTable[NextNum] = { Kills needed, "weapon class" } WeaponsTable[1] = { 1, "weapon_glock" } WeaponsTable[2] = { 2, "weapon_deagle" } WeaponsTable[3] = { 3, "weapon_ak47" } hook.Add( "PlayerSpawn", "Reset Kills", function( ply ) if Reset_On_Death then ply.ReqKills = 0 end end ) hook.Add( "PlayerDeath", "Add Kills", function( victim, inf, killer ) if victim:IsPlayer() and killer:IsPlayer() then if victim == killer then return end killer.ReqKills = killer.ReqKills + 1 for _,v in pairs( WeaponsTable ) do if killer.ReqKills == v[1] then killer:Give( v[2] ) break end end end end ) [/lua]
Hey thanks a lot guys i appreciate it
[QUOTE=Claiver;35599332]- You would have to keep track of the current amount of damage done for a player, you would do this in a variable. [/QUOTE] It would be better if you just kept track of the kills.. Because if someone kills another person that has 10 hp they should still be rewarded the kill.
[QUOTE=.\\Shadow};35600882]I think he was looking for something like this: [lua] local Reset_On_Death = true -- Set to false if you don't want the player's amount of kills to be reset when they die. local WeaponsTable = {} -- WeaponsTable[NextNum] = { Kills needed, "weapon class" } WeaponsTable[1] = { 1, "weapon_glock" } WeaponsTable[2] = { 2, "weapon_deagle" } WeaponsTable[3] = { 3, "weapon_ak47" } hook.Add( "PlayerSpawn", "Reset Kills", function( ply ) if Reset_On_Death then ply.ReqKills = 0 end end ) hook.Add( "PlayerDeath", "Add Kills", function( victim, inf, killer ) if victim:IsPlayer() and killer:IsPlayer() then if victim == killer then return end killer.ReqKills = killer.ReqKills + 1 for _,v in pairs( WeaponsTable ) do if killer.ReqKills == v[1] then killer:Give( v[2] ) break end end end end ) [/lua][/QUOTE] sorry about this there is something i forgot to mention, i want the 2 teams i have to get different sets of weapons on the kills they get
[QUOTE=NA_PREDATOR;35602011]sorry about this there is something i forgot to mention, i want the 2 teams i have to get different sets of weapons on the kills they get[/QUOTE] [lua] local Reset_On_Death = true -- Set to false if you don't want the player's amount of kills to be reset when they die. local WeaponsTable = {} -- WeaponsTable[NextNum] = { Kills needed, "weapon class", TEAM_NAME } WeaponsTable[1] = { 1, "weapon_glock", TEAM_RED } WeaponsTable[2] = { 1, "weapon_superglock", TEAM_BLUE } WeaponsTable[3] = { 2, "weapon_deagle", TEAM_RED } WeaponsTable[4] = { 2, "weapon_glowstick", TEAM_BLUE } WeaponsTable[5] = { 3, "weapon_baton", TEAM_RED } WeaponsTable[6] = { 3, "weapon_ak47", TEAM_BLUE } hook.Add( "PlayerSpawn", "Reset Kills", function( ply ) if Reset_On_Death then ply.ReqKills = 0 end end ) hook.Add( "PlayerDeath", "Add Kills", function( victim, inf, killer ) if victim:IsPlayer() and killer:IsPlayer() then if victim == killer then return end if not killer.ReqKills then killer.ReqKills = 1 else killer.ReqKills = killer.ReqKills + 1 end for _,v in pairs( WeaponsTable ) do if killer.ReqKills == v[1] and killer:Team() == v[3] then killer:Give( v[2] ) break end end end end ) [/lua]
Hook 'Add Kills' Failed: [gamemodes\bullet_time\gamemode\init.lua:33] attempt to perform arithmetic on field 'ReqKills' (a nil value)
[QUOTE=NA_PREDATOR;35602315]Hook 'Add Kills' Failed: [gamemodes\bullet_time\gamemode\init.lua:33] attempt to perform arithmetic on field 'ReqKills' (a nil value)[/QUOTE] Sorry, updated the post above with the fixed version.
It works but i wanted it to keep the new weapons even when you die, i assumed that was what the first line of code was for
[QUOTE=NA_PREDATOR;35602631]It works but i wanted it to keep the new weapons even when you die, i assumed that was what the first line of code was for[/QUOTE] [lua] local Reset_On_Death = false -- If set to true, the player's amount of kills and all the weapons they earned with said kills will be reset when the player spawns. local WeaponsTable = {} -- WeaponsTable[NextNum] = { Kills needed, "weapon class", TEAM_NAME } WeaponsTable[1] = { 1, "weapon_glock", TEAM_RED } WeaponsTable[2] = { 1, "weapon_superglock", TEAM_BLUE } WeaponsTable[3] = { 2, "weapon_deagle", TEAM_RED } WeaponsTable[4] = { 2, "weapon_glowstick", TEAM_BLUE } WeaponsTable[5] = { 3, "weapon_pumpshotgun", TEAM_RED } WeaponsTable[6] = { 3, "weapon_ak47", TEAM_BLUE } hook.Add( "PlayerSpawn", "Reset Kills", function( ply ) if Reset_On_Death then ply.ReqKills = 0 ply.EarnedWeps = {} else if not ply.EarnedWeps then ply.EarnedWeps = {} end if ply.EarnedWeps[1] then for _,v in pairs( ply.EarnedWeps ) do ply:Give( v ) end end end end ) hook.Add( "PlayerDeath", "Add Kills", function( victim, inf, killer ) if victim:IsPlayer() and killer:IsPlayer() then if victim == killer then return end if not killer.ReqKills then killer.ReqKills = 1 killer.EarnedWeps = {} else killer.ReqKills = killer.ReqKills + 1 end for _,v in pairs( WeaponsTable ) do if killer.ReqKills == v[1] and killer:Team() == v[3] then killer:Give( v[2] ) table.insert( killer.EarnedWeps, v[2] ) break end end end end ) [/lua]
Hook 'Reset Kills' Failed: [gamemodes\bullet_time\gamemode\init.lua:23] attempt to index field 'EarnedWeps' (a nil value) also thanks alot for staying with me i appreciate how much effort you're putting into helping me
Updated my post, see if that works.
it didn't work and this time i get no error messages here is what my init.lua looks like not sure if it can help [lua]AddCSLuaFile( "cl_init.lua" ) //Tell the server that the client need to download cl_init.lua AddCSLuaFile( "shared.lua" ) //Tell the server that the client need to download shared.lua include( 'shared.lua' ) //Tell the server to load shared.lua function GM:PlayerInitialSpawn( ply ) //"When the player first joins the server and spawns" function local Reset_On_Death = false -- Set to false if you don't want the player's amount of kills to be reset when they die. local WeaponsTable = {} -- WeaponsTable[NextNum] = { Kills needed, "weapon class", TEAM_NAME } WeaponsTable[1] = { 30, "bt_mp5", 1 } WeaponsTable[2] = { 30, "bt_tmp", 2 } WeaponsTable[3] = { 70, "bt_para", 1 } WeaponsTable[4] = { 70, "bt_deagle", 2 } hook.Add( "PlayerSpawn", "Reset Kills", function( ply ) if Reset_On_Death then ply.ReqKills = 0 ply.EarnedWeps = {} else if not ply.EarnedWeps then ply.EarnedWeps = {} end if ply.EarnedWeps[1] then for _,v in pairs( ply.EarnedWeps ) do ply:Give( v ) end end end end ) hook.Add( "PlayerDeath", "Add Kills", function( victim, inf, killer ) if victim:IsPlayer() and killer:IsPlayer() then if victim == killer then return end if not killer.ReqKills then killer.ReqKills = 1 killer.EarnedWeps = {} else killer.ReqKills = killer.ReqKills + 1 end for _,v in pairs( WeaponsTable ) do if killer.ReqKills == v[1] and killer:Team() == v[3] then killer:Give( v[2] ) table.insert( killer.EarnedWeps, v[2] ) break end end end end ) ply:ConCommand( "team_menu" ) //Run the console command when the player first spawns end //End the "when player first joins server and spawn" function function GM:PlayerLoadout(ply) --"The weapons/items that the player spawns with" function ply:StripWeapons() -- This command strips all weapons from the player. if ply:Team() == 1 then --If the player is on team "Guest"... ply:Give( "bt_fiveseven" ) ply:SetModel( "models/player/barney.mdl" ) elseif ply:Team() == 2 then -- Otherwise, if the player is on team "Another Guest"... ply:Give( "bt_glock" ) -- ...then give them the Phys Gun. ply:SetModel( "models/player/gman_high.mdl" ) end -- This ends the if/elseif. end -- This ends the function. function team_1( ply ) ply:SetTeam( 1 ) //Make the player join team 1 ply:Spawn() end function team_2( ply ) ply:SetTeam( 2 ) //Make the player join team 2 ply:Spawn() end concommand.Add( "team_1", team_1 ) //Add the command to set the players team to team 1 concommand.Add( "team_2", team_2 ) //Add the command to set the players team to team 2 [/lua]
It shouldn't be inside the PlayerInitialSpawn function.
where should it be?
[QUOTE=NA_PREDATOR;35603409]where should it be?[/QUOTE] Any serverside file; it doesn't need to be inside any function. You can just place it at the end of your init file.
Ahh sorry, I was working on a particular system myself which works based on damage, hence I used it in my explanation. Hope you can work something out with the code-snippets supplied by the above users! :]
[QUOTE=Claiver;35605897]Ahh sorry, I was working on a particular system myself which works based on damage, hence I used it in my explanation. Hope you can work something out with the code-snippets supplied by the above users! :][/QUOTE] thats fine its still nice that you tried to help shadow, i did what you said and its still not working
I didn't have anyone to test it with so i just typed kill in the console test it, could that have had something to do with it? [editline]17th April 2012[/editline] don't worry i figured it out and it works perfectly now, it was the line [lua]function GM:PlayerLoadout(ply) --"The weapons/items that the player spawns with" function ply:StripWeapons()[/lua] thank you very much for all the help shadow
Why don't you guys just use playerEntity:Frags() rather than storing the frags in a variable.
[QUOTE=Chessnut;35616957]Why don't you guys just use playerEntity:Frags() rather than storing the frags in a variable.[/QUOTE] You may wish to reset the "kill count" on death (and with that the unlocked weapons) but still keep the total kill count (which could be Frags, true)
[QUOTE=Claiver;35619891]You may wish to reset the "kill count" on death (and with that the unlocked weapons) but still keep the total kill count (which could be Frags, true)[/QUOTE] You don't have to do that because he doesn't want it to reset.
Sorry, you need to Log In to post a reply to this thread.