Need some help with weapon spawning

Hey guys so I wrote some code that’s supposed to give a player a certain weapon when their score gets to an x amount. The code works but the problem is that when they get to the certain score they need to die and spawn again to get the weapon. The problem is that I need the player to get the weapon right when he gets to x score. Any ideas? all help is appreciated :slight_smile:

P.S This is my first time sitting down and trying to code something serious so forgive me if I sound like an idiot :o.

Forgot to mention that the giving of weapons part is not done yet but it’s near the bottom of the code.


AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
 
include( 'shared.lua' )

function GM:PlayerInitialSpawn( ply )
	   ply:PrintMessage( HUD_PRINTTALK, "Welcome to Arms Race, " .. ply:Name() .. "!" )
	   ply:SetGravity  ( .85 )  
	   ply:SetWalkSpeed( 250 )  
	   ply:SetRunSpeed ( 350 )
	   ply:SetTeam( 2 )
end


function GM:PlayerSpawn( ply )
self.BaseClass:PlayerSpawn( ply ) 
end

function GM:PlayerLoadout ( ply )

if ply:Team() == 2 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_pp19" )
end
if ply:Team() == 3 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_uzi" )
end		
if ply:Team() == 4 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_mac11" )
end
if ply:Team() == 5 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_mp5a5" )
end
if ply:Team() == 6 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_m4a1" )
end
if ply:Team() == 7 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_ak47" )
end
if ply:Team() == 8 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_sg550" )
end
if ply:Team() == 9 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_g36c" )
end
if ply:Team() == 10 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_m3s90" )
end
if ply:Team() == 11 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_toz34" )
end
if ply:Team() == 12 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_ks23" )
end
if ply:Team() == 13 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_rem870" )
end
if ply:Team() == 14 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_m82" )
end
if ply:Team() == 15 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_sr25" )
end
if ply:Team() == 16 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_m14" )
end
if ply:Team() == 17 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_m24" )
end
if ply:Team() == 18 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_rpk" )
end
if ply:Team() == 19 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_deagle" )
end
if ply:Team() == 20 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_p226" )
end
if ply:Team() == 21 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_ragingbull" )	
end
if ply:Team() == 22 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_glock20" )
end
if ply:Team() == 23 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_m67" )
end
if ply:Team() == 24 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_m79" )
end
if ply:Team() == 25 then
		ply:Give( "fas2_machete" )
end
if ply:Team() == 26 then
		ply:Give( "weapon_jihadbomb" )
end
hook.Add("PlayerDeath","CheckKillCount",function( ply )
if ply:Frags()>1 then
	ply:SetTeam( 3 )
end
if ply:Frags()>3 then
	ply:SetTeam( 4 )
end
if ply:Frags()>5 then
	ply:SetTeam( 5 )
end
if ply:Frags()>7 then
	ply:SetTeam( 6 )
end
if ply:Frags()>9 then
	ply:SetTeam( 7 )
end
if ply:Frags()>11 then
	ply:SetTeam( 8 )
end
if ply:Frags()>13 then
	ply:SetTeam( 9 )
end
if ply:Frags()>15 then
	ply:SetTeam( 10 )
end
if ply:Frags()>17 then
	ply:SetTeam( 11 )
end
if ply:Frags()>19 then
	ply:SetTeam( 12 )
end
if ply:Frags()>21 then
	ply:SetTeam( 13 )
end
if ply:Frags()>23 then
	ply:SetTeam( 14 )
end
if ply:Frags()>25 then
	ply:SetTeam( 15 )
end
if ply:Frags()>27 then
	ply:SetTeam( 16 )
end
if ply:Frags()>29 then
	ply:SetTeam( 17 )
end
if ply:Frags()>31 then
	ply:SetTeam( 18 )
end
if ply:Frags()>33 then
	ply:SetTeam( 19 )
end
if ply:Frags()>35 then
	ply:SetTeam( 20 )
end
if ply:Frags()>37 then
	ply:SetTeam( 21 )
end
if ply:Frags()>39 then
	ply:SetTeam( 22 )
end
if ply:Frags()>41 then
	ply:SetTeam( 23 )
end
if ply:Frags()>43 then
	ply:SetTeam( 24 )
end
if ply:Frags()>45 then
	ply:SetTeam( 25 )
end
if ply:Frags()>47 then
	ply:SetTeam( 26 )
end
end)
end

Use GM:PlayerLoadout for this stuff, man

Kk. I just put all the team stuff under function GM:PlayerLoadout, thanks. Also, what’s the difference between GM:PlayerLoadout and GM:PlayerSpawn?

Try changing the end to and elseif like this. I don’t know why it isnt spawning for that statment


elseif ply:Team() == 2 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_pp19" )
end
elseif ply:Team() == 3 then
		ply:Give( "fas2_dv2" )
		ply:Give( "fas2_uzi" )
end

		

Well they do spawn but the player has to die and spawn again for him to get the weapons :confused:

Use PlayerDeath hook; it takes arguments ( victim, inflictor, attacker ) – The inflictor can be a weapon, and the attacker can be a player among other things. Upgrade the attacker when they get x kills. ( You can do a hook.Call( “PlayerLoadout”, GAMEMODE, attacker ) inside PlayerDeath after you change their team )

Also, the big if/elseif jungle you have there could be coded much differently…

Why do you change their team? If it’s to give them different weapons, why not just use the score number instead of team number? Why not use a table to store the kills needed for x upgrade instead of the huge jungle?

So brandonj4 actually suggests that you edit player classes for this instead.

Well I tried using the score numbers and I ran into another wall. Whenever I would get to x kills I would get the weapon in that instant ,but I wouldn’t spawn with it. (I thought it was funny when I ran into the exact opposite problem using teams lol.) And like I said i’m pretty new to coding and I read something about using tables to store information but I had no idea on how to do it. Could you explain how I could make a table for this? :o

[editline]16th March 2014[/editline]

What do you mean?

Players have their own classes too. You can assign a class by checking the team of the player.

just to throw it out there, theres no need to use 2 different “if” statements for setting the team and then the guns.



if ply:Frags()>1 then
	ply:SetTeam( 3 )
        ply:Give( "fas2_dv2" )
  	ply:Give( "fas2_uzi" )
end


Also when using the playerdeath hook it uses the victim, inflictor, and attacker arguments. From what i read you want to give the attacker the weapon so you would do:



hook.Add("PlayerDeath", "CheckKillCount", function(vic, infl, atk)
	if atk:Frags() >= 1 && !atk:Frags() > 3 then
		atk:SetTeam( 3 )
                atk:Give( "fas2_dv2" )
  		atk:Give( "fas2_uzi" )
  	end
end)


Not sure but i think having a frag amount of say 30 would straight fuck everything since you were using only greater-than’s
Also im confused as to the point of having 30 different teams when you could just save their frags and then check them every playerdeath hook, and i guess ill throw out how id code it because im bored. of course idek if this works or if its the best way to do it but oh well it would be my way.



hook.Add("PlayerDeath", "CheckKillCount", function(vic, infl, atk)
	atk:SetPData("killcount", atk:Frags())
	local checkKC = atk:GetPData("killcount")
	if checkKC == 1 then
        atk:Give( "fas2_dv2" )
  		atk:Give( "fas2_uzi" )
  	elseif checkKC == 3 then
  		--give more guns
  	elseif checkKC == 5 then
  		--blah blah guns
  	end
end)


Feel free to correct my if I fucked something up :v:

Well when I had it set up using only the player’s frags they would get the weapon but would never spawn with it. It would check the player’s frags only when they got a kill. Thanks for the advice though . I probably won’t use teams since you gave me the idea to use this instead:


hook.Add("PlayerDeath", "CheckKillCount", function(vic, infl, atk)
	if atk:Frags() >= 1 && !atk:Frags() > 3 then
                atk:Give( "fas2_dv2" )
  		atk:Give( "fas2_uzi" )
  	end
end)

And why would everything go to shit when a player’s frags got to 30? :o

Not in that code but the code before, how it had if a > 1 elseif b > 3 elseif c >5 etc having 30 kills it wouldnt know whether to give you a, b, c, d, etc. Not sure how it would work but its just speculation

If you’re using greater-thans, then you call the highest number first…

[lua]if blah > 30 then

elseif blah > 29 then

elseif blah > 25 then

else

end[/lua]

But, doing that is a bleh idea… Setting teams just to upgrade weapons is also bleh…

You could make a table, and define the key as the amount of kills needed. You can get away with using == or if ( TheTable[ Player:Frags( ) ] ) then … end where … is a for loop to go through the weapons it gives.

[lua]// Table of weapons to give, key is number of kills == // If done in order, on PlayerLoadout you could go through the table looking for the correct index, OR set the key the player is at when they get an upgrade…
local UpgradesTable = { };
UpgradesTable[ 1 ] = { “weapon_1”, “weapon_sidearm_1”, “weapon_grenade1” };
UpgradesTable[ 5 ] = { “weapon_2”, “weapon_sidearm_2”, “weapon_grenade1” };
UpgradesTable[ 10 ] = { “weapon_3”, “weapon_sidearm_3”, “weapon_grenade1” };
UpgradesTable[ 25 ] = { “weapon_4”, “weapon_sidearm_4”, “weapon_grenade1” };
UpgradesTable[ 50 ] = { “weapon_5”, “weapon_sidearm_5”, “weapon_grenade1” };

// This will run on death, and it’ll update the UpgradeIndex which we use for Respawn gives, this handles the instantaneous upgrades
hook.Add( “PlayerDeath”, “GonnaGitMeSomeWeaponUpgrades”, function( victim, inflictor, attacker )
if ( !attacker:IsPlayer( ) ) then return; end

if  ( UpgradesTable[ attacker:Frags( ) ] ) then
	for k, v in pairs( UpgradesTable[ attacker:Frags( ) ] ) do
		attacker:Give( v );
	end
	attacker.__UpgradeIndex = attacker:Frags( );
end

end );

// This will run on top of the default, so we only have to deal with upgrades
hook.Add( “PlayerLoadout”, “GiveUpgradesOnSpawn”, function( _p )
if ( _p.__UpgradeIndex ) then
for k, v in pairs( UpgradesTable[ _p.__UpgradeIndex ] ) do
_p:Give( v );
end
end
end );[/lua]

Hey Ace thanks man this worked beautifully but now my issue is that I need a player to spawn with a gun when they join the server and have a score of 0. The problem is that when I make the UpgradesTable[ 0 ] the player spawns with no weapons. Any ideas?

It’s because of how the loadout is setup.

The boolean statement ( _p.__UpgradeIndex ) will not exist.

To rectify that, you can do if ( !_p.__UpgradeIndex ) then _p.__UpgradeIndex = 0; end
at the top of the PlayerLoadout hook right before the if statement. With it set to 0 you’ll need an UpgradesTable[ 0 ] = { … }

Alternatively, you can add an else to the if statement which currently exists and hard-code the default weapons in there.

So it would be


hook.Add( "PlayerLoadout", "GiveUpgradesOnSpawn", function( _p )
if ( !_p.__UpgradeIndex ) then _p.__UpgradeIndex = 0; end
       if ( _p.__UpgradeIndex ) then

and the table would read


       
local UpgradesTable = { };

UpgradesTable[ 0 ] = { "fas2_pp19", "fas2_dv2" }; 
UpgradesTable[ 2 ] = { "fas2_uzi", "fas2_dv2" };
UpgradesTable[ 4 ] = { "fas2_mac11", "fas2_dv2" };

Pretty much; but now that we’ve added a default, we no longer need to check for: [lua]if ( _p.__UpgradeIndex ) then[/lua]

in PlayerLoadout. We can now swap it for something more meaningful:

[lua]//
// This will run on top of the default, so we only have to deal with upgrades
//
hook.Add( “PlayerLoadout”, “GiveUpgradesOnSpawn”, function( _p )
// Initialize the var
if ( !_p.__UpgradeIndex ) then _p.__UpgradeIndex = 0; end

// Since we initialized it, let's just verify that the key / index exists to prevent errors...
// Alternatively, you could leave this out entirely if you know for a fact you set it up properly.
if ( UpgradesTable[ _p.__UpgradeIndex ] ) then
	for k, v in pairs( UpgradesTable[ _p.__UpgradeIndex ] ) do
		_p:Give( v );
	end
end

end );[/lua]