Why won't this work

I’m trying to make a pointshop item that converts some items into better items. Unfortunently it doesn’t always work. Radiation gun and others spew errors when you equip them and I’m at my wits end to figure out why


ITEM.Name = 'Upgrader'
ITEM.Price = 50000
ITEM.Model = 'models/props_c17/SuitCase_Passenger_Physics.mdl'
ITEM.Bone = 'ValveBiped.Bip01_Spine2'

function ITEM:OnEquip(ply, modifications)
	ply:PS_AddClientsideModel(self.ID)
end

function ITEM:OnHolster(ply)
	ply.CanChange = false
	ply:PS_RemoveClientsideModel(self.ID)
end

function ITEM:ModifyClientsideModel(ply, model, pos, ang)
	model:SetModelScale(0.9, 0)
	pos = pos + (ang:Right() * 5) + (ang:Up() * 6) + (ang:Forward() * 2)
	
	return model, pos, ang
end

function ITEM:Think(ply, modifications)
   if ply.CanChange == true then

	if ply:HasWeapon( 'weapon_ttt_headcrab' ) then
	ply:PrintMessage(HUD_PRINTTALK, "beep")
		ply:StripWeapon('weapon_ttt_headcrab')
	ply:PrintMessage(HUD_PRINTTALK, "boop")
		ply:Give('mirv')
	ply:PrintMessage(HUD_PRINTTALK, "WEAPON UPGRADED")
--		ply:SelectWeapon('weapon_ttt_fme')
--	ply:PrintMessage(HUD_PRINTTALK, "Selected")
	end



	if ply:HasWeapon( 'weapon_radiation' ) then
	ply:PrintMessage(HUD_PRINTTALK, "Scanned")
		ply:StripWeapon('weapon_radiation')
	ply:PrintMessage(HUD_PRINTTALK, "Stripped")
		ply:Give('bio_gun')
	ply:PrintMessage(HUD_PRINTTALK, "Given")

	end

	if ply:HasWeapon( 'weapon_ttt_c4' ) then
	ply:PrintMessage(HUD_PRINTTALK, "beep")
		ply:StripWeapon('weapon_ttt_c4')
	ply:PrintMessage(HUD_PRINTTALK, "boop")
		ply:Give('w_upc4')
	ply:PrintMessage(HUD_PRINTTALK, "WEAPON UPGRADED")
--		ply:SelectWeapon('w_upc4')
--	ply:PrintMessage(HUD_PRINTTALK, "Selected")
	end

	if ply:HasWeapon( 'goat_launcher' ) then
	ply:PrintMessage(HUD_PRINTTALK, "beep")
		ply:StripWeapon('goat_launcher')
	ply:PrintMessage(HUD_PRINTTALK, "boop")
		ply:Give('up_goat_launcher')
	ply:PrintMessage(HUD_PRINTTALK, "WEAPON UPGRADED")
--		ply:SelectWeapon('goat_launcher')
--	ply:PrintMessage(HUD_PRINTTALK, "Selected")
	end

	if ply:HasWeapon( 'weapon_ttt_adv_disguiser' ) then
	ply:PrintMessage(HUD_PRINTTALK, "beep")
		ply:StripWeapon('weapon_ttt_adv_disguiser')
	ply:PrintMessage(HUD_PRINTTALK, "boop")
		ply:Give('doppelbanger')
	ply:PrintMessage(HUD_PRINTTALK, "WEAPON UPGRADED")
--		ply:SelectWeapon('goat_launcher')
--	ply:PrintMessage(HUD_PRINTTALK, "Selected")
	end

	if ply:HasWeapon( 'weapon_ttt_biggun' ) then
	ply:PrintMessage(HUD_PRINTTALK, "beep")
		ply:StripWeapon('weapon_ttt_biggun')
	ply:PrintMessage(HUD_PRINTTALK, "boop")
		ply:Give('up_biggun')
	ply:PrintMessage(HUD_PRINTTALK, "WEAPON UPGRADED")
		ply:SelectWeapon('up_biggun')
--	ply:PrintMessage(HUD_PRINTTALK, "Selected")
	end

	if ply:HasWeapon( 'weapon_ttt_manhacknade' ) then
	ply:PrintMessage(HUD_PRINTTALK, "beep")
		ply:StripWeapon('weapon_ttt_manhacknade')
	ply:PrintMessage(HUD_PRINTTALK, "boop")
		ply:Give('weapon_ttt_goathacknade')
	ply:PrintMessage(HUD_PRINTTALK, "WEAPON UPGRADED")
		ply:SelectWeapon('weapon_ttt_goathacknade')
	end
   end
end

hook.Add("TTTBeginRound", "reset.ourvariable", function()
     for _, plys in ipairs(player.GetAll()) do
          plys.CanChange = true
     end
end)


hook.Add("TTTEndRound", "set.ourvariable", function()
     for _, plys in ipairs(player.GetAll()) do
          plys.CanChange = false
     end
end)

here are the radiation gun’s errors

I’m assuming it’s trying to call the strip weapon function from the client, in the think hook check if it’s the server, if it’s not don’t execute the code in that hook.

surrounding the “if ply.CanChange == true then” statement with an “if SERVER then” didn’t help, it just made it so it didn’t work at all.

UPDATE now even weapons that worked before don’t work. This is intensely frustrating,

you aren’t running it serverside then - fix that

How do I do that?

autorun/server

I don’t understand how this will help me, I’ve been putting it in the think function of a pointshop item. How do I get from there to autorun/server?

Any help is appreciated

Here, I just recoded it. I removed duplicate code and the silly boop beep stuff which didn’t really matter. You could add timers and set up say 3 seconds in a row where that happens with noises playing if you want.

https://dl.dropboxusercontent.com/u/26074909/tutoring/pointshop/item_upgrader.lua

A heads up; try to make more descriptive thread titles. It’s hard to know what you need help with when it’s just “Help help!”; we don’t know if you’ve fallen and can’t get up, or if it’s a point-shop upgrader item or whatever else.

Yeah, sorry, I set the beep boop crap up in development and people liked it. I like it better without it as well.
First I want to tell you I really appreciate all the work you put into that for me and I don’t want to sound ungrateful at all because I can tell you’re a ton better at this than I am.
Unfortunately, though, it doesn’t work, and it’s not returning any errors :confused:
-edit-
Should I make the weapons to be upgraded strings?
-edit-
No, I see you’ve handled that… It lags for a second as I switch. Maybe something’s happening there.

Yeah, I removed it from the Think hook and made it upgrade when they switch to that weapon. I return true in that hook so it cancels switching to the weapon then have the server give them the new weapon and switch to it the next frame.

Make sure the weapons exist. And you’re saying absolutely 0 errors in console both server and client?

Add a few print statements to see how far it’s getting?

I tried that and none of them ran, I can only conclude that the hook’s not getting called.

Add a print before: if ( _newWeapon == NULL ) then return false; end

Just to see something. It’s possible it’s glitching something; but the hook is shared so it should be called unless a differently identified hook of the same type is calling return. Hooks are executed before the GM: function, and if a hook returns something it will prevent the next hook in line to call along with the GM function.

Do a search for your addons and game-mode files for the hook name: PlayerSwitchWeapon

See if there are other instances of it.

There’s a good chance your script doesn’t work because you constantly call a variable that doesn’t exist.

Change all instances of _upgrade to _wToUpgrade

Oops, thanks! I originally had it as _upgrade but changed it. It’s updated: https://dl.dropboxusercontent.com/u/26074909/tutoring/pointshop/item_upgrader.lua

Ok, based on my prints where it is failing now is
“if ( ITEM.__UpgradeTable[ _wToUpgrade ] ) then”

Ok, switched that to a table in player and defined it on equip and now I get this when I ran it with C4. It did manage to remove the C4, which is something.

Current Code



//
// Recoded from the ground up to serve as a tutorial - UNTESTED - Josh 'Acecool' Moser
//
ITEM.Name = 'Upgrader'
ITEM.Price = 50000
ITEM.Model = 'models/props_c17/SuitCase_Passenger_Physics.mdl'
ITEM.Bone = 'ValveBiped.Bip01_Spine2'


function ITEM:OnEquip( ply, modifications )
	ply:PS_AddClientsideModel( self.ID );
	ply.__UpgradeTable = {
	// Weapon to be upgraded	= Upgraded weapon
	weapon_ttt_headcrab 		= "mirv";
	weapon_radiation 			= "bio_gun";
	weapon_ttt_c4 				= "w_upc4";
	goat_launcher 				= "up_goat_launcher";
	weapon_ttt_adv_disguiser 	= "doppelbanger";
	weapon_ttt_biggun 			= "up_biggun";
	weapon_ttt_manhacknade 		= "weapon_ttt_goathacknade";
	
	// Follow the pattern to add new upgrades.
};
end

function ITEM:OnHolster( ply )
	ply.CanChange = false;
	ply:PS_RemoveClientsideModel( self.ID );
	ply.__UpgradeTable = null
end

function ITEM:ModifyClientsideModel( ply, model, pos, ang )
	model:SetModelScale( 0.9, 0 );
	pos = pos + ( ang:Right( ) * 5 ) + ( ang:Up( ) * 6 ) + ( ang:Forward( ) * 2 );

	return model, pos, ang;
end




//
// Upgrade weapons when they switch to it.
//
hook.Add( "PlayerSwitchWeapon", "ItemUpgrader:PlayerSwitchWeapon", function( _p, _oldWeapon, _newWeapon )

--	_p:PrintMessage(HUD_PRINTTALK, "hook")
	if ( _newWeapon == NULL ) then return false; end
--	_p:PrintMessage(HUD_PRINTTALK, "check weapon")
	if ( _p.CanChange ) then
--		_p:PrintMessage(HUD_PRINTTALK, "Can Change")
		local _wToUpgrade = string.lower( _newWeapon:GetClass( ) );
--		if ( ITEM.__UpgradeTable[ _wToUpgrade ] ) then
		if ( 	_p.__UpgradeTable[ _wToUpgrade ] ) then
		_p:PrintMessage(HUD_PRINTTALK, "Upgradable")
			if ( SERVER ) then
				_p:PrintMessage(HUD_PRINTTALK, "Serverside")
				// Server calls: Strip it, ensure it's removed, give them the upgrade
				_p:StripWeapon( _wToUpgrade );
				SafeRemoveEntity( _newWeapon );
				_p:Give( ITEM.__UpgradeTable[ _wToUpgrade ] );
			end

			// Tell them about the upgrade, wait 1 frame Then select the new weapon.
			_p:PrintMessage( HUD_PRINTTALK, "Your old weapon has been upgraded into something good!" );			

			timer.Simple( 0, function( )
				_p:SelectWeapon( ITEM.__UpgradeTable[ _wToUpgrade ] );
			end );

			// Only allow one upgrade? Comment out if you want to allow unlimited upgrades.
--			_p.CanChange = false;

			// Prevent the switch to the new weapon we removed.
			return true;
		end
	end
end );


//
// Allow upgrades to occur only during an active round.
//
hook.Add( "TTTBeginRound", "Perks:TTTBeginRound", function( )
     for k, _p in ipairs( player.GetAll( ) ) do
          _p.CanChange = true;
     end
end );


//
// Allow upgrades to occur only during an active round.
//
hook.Add( "TTTEndRound", "Perks:TTTEndRound", function( )
     for k, _p in ipairs( player.GetAll( ) ) do
          _p.CanChange = false;
     end
end );


Any help would be much appreciated

move this code outside of the equip function:
[lua]
ply.__UpgradeTable = {
// Weapon to be upgraded = Upgraded weapon
weapon_ttt_headcrab = “mirv”;
weapon_radiation = “bio_gun”;
weapon_ttt_c4 = “w_upc4”;
goat_launcher = “up_goat_launcher”;
weapon_ttt_adv_disguiser = “doppelbanger”;
weapon_ttt_biggun = “up_biggun”;
weapon_ttt_manhacknade = “weapon_ttt_goathacknade”;

// Follow the pattern to add new upgrades.

}[/lua]

and change it to ITEM.

and change this to self:
if ( _p.__UpgradeTable[ _wToUpgrade ] ) then

You’ve been babied and handed code so far, if you can’t figure this simple thing out obviously you need to go learn Lua.