• Why won't this work
    33 replies, posted
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 [CODE]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)[/CODE] here are the radiation gun's errors [QUOTE][ERROR] addons/pointshop-master/lua/items/perks/upgrader.lua:39: attempt to call method 'StripWeapon' (a nil value) 1. unknown - addons/pointshop-master/lua/items/perks/upgrader.lua:39 2. fn - addons/pointshop-master/lua/sh_pointshop.lua:132 3. unknown - addons/ulib/lua/ulib/shared/hook.lua:183 B[/QUOTE]
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
[QUOTE=MeepDarknessM;44170773]you aren't running it serverside then - fix that[/QUOTE] How do I do that?
[QUOTE=Galactic;44170857]How do I do that?[/QUOTE] autorun/server
[QUOTE=MrPlonker;44171663]autorun/server[/QUOTE] 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. [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/pointshop/item_upgrader.lua[/url] 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 :/ -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
[QUOTE=OzymandiasJ;44192211]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[/QUOTE] Oops, thanks! I originally had it as _upgrade but changed it. It's updated: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/pointshop/item_upgrader.lua[/url]
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. [QUOTE][ERROR] addons/pointshop-master/lua/items/perks/upgrader.lua:70: attempt to index field '__UpgradeTable' (a nil value) 1. fn - addons/pointshop-master/lua/items/perks/upgrader.lua:70 2. unknown - addons/ulib/lua/ulib/shared/hook.lua:183 [/QUOTE] Current Code [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 ); [/CODE]
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.
[QUOTE=OzymandiasJ;44224279]move this code outside of the equip function: 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.[/QUOTE] If you had read my post you'd realize that putting it back where it was just means that it would fail at the same point it was already failing at. The switch weapons hook doesn't know what "Item" is or which pointshop item to pull it from, or even which player to scan for it if it's not defined. At least I'll admit to being a poor lua coder. You need to squelsh your superiority complex and just stop being unhelpful until Acecool gets back.
[QUOTE=Galactic;44229253] At least I'll admit to being a poor lua coder. You need to squelsh your superiority complex and just stop being unhelpful until Acecool gets back.[/QUOTE] It's not a "superiority complex" the rules in this section specifically state that this isn't an area for us to code for you, it's for help with code. You've been given code and there is a simple error that you can't even recognize. The current error your having is because you didn't read my post and just assumed I was trying to be unhelpful when I gave you the answer. Which is the fact that you're still refrencing ITEM.__UpgradeTable which doesn't exist because you moved it to being refrenced on the player. You're calling a table on the player which will never exist on them until they activate the item. You can move the _p.CanChange = true into the OnEquip function and then do [lua]if _p.CanChange and GetRoundState() == ROUND_ACTIVE then [/lua] in the PlayerSelectWeapon hook. Then the TTTBegin/EndRound hooks at the bottom can be deleted. You really should be referencing the item table directly as an upvalue instead of declaring it on every player though.
good god acecool, your unnecessary _'s in front of your variables hurts my eyes xD.
[QUOTE=OzymandiasJ;44233393]It's not a "superiority complex" the rules in this section specifically state that this isn't an area for us to code for you, it's for help with code. You've been given code and there is a simple error that you can't even recognize. The current error your having is because you didn't read my post and just assumed I was trying to be unhelpful when I gave you the answer. Which is the fact that you're still refrencing ITEM.__UpgradeTable which doesn't exist because you moved it to being refrenced on the player. You're calling a table on the player which will never exist on them until they activate the item. You can move the _p.CanChange = true into the OnEquip function and then do [lua]if _p.CanChange and GetRoundState() == ROUND_ACTIVE then [/lua] in the PlayerSelectWeapon hook. Then the TTTBegin/EndRound hooks at the bottom can be deleted. You really should be referencing the item table directly as an upvalue instead of declaring it on every player though.[/QUOTE] This is useful to me. Thank you. I'm sorry I was so defensive earlier.
Ok, so here is the current code... [CODE]// // Recoded from the ground up to serve as a tutorial - NONFUNCTIONAL- Josh 'Acecool' Moser //Edited and fiddled with by Galactic 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.Upgrader = true; // Follow the pattern to add new upgrades. end function ITEM:OnHolster( ply ) ply.CanChange = false; ply.Upgrader = 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 // // 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 ) and (_p.Upgrader) then -- _p:PrintMessage(HUD_PRINTTALK, "Can Change") local _wToUpgrade = string.lower( _newWeapon:GetClass( ) ); -- if ( ITEM.__UpgradeTable[ _wToUpgrade ] ) then local 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. } if ( 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( 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( 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 );[/CODE] and here is the error it produces when someone switches weapons [QUOTE]master/lua/items/perks/upgrader.lua:61: attempt to index global 'UpgradeTable' (a nil value) 1. fn - addons/pointshop-master/lua/items/perks/upgrader.lua:61 2. unknown - addons/ulib/lua/ulib/shared/hook.lua:183 3. SelectWeapon - [C]:-1 4. unknown - gamemodes/terrortown/gamemode/weaponry.lua:196 5. unknown - lua/includes/modules/concommand.lua:69[/QUOTE]
UpgradeTable needs to be defined outside of the hook; at the top of the file as the original did...
Can I define it as a global since it wasn't working as part of the pointshop item?
[QUOTE=Galactic;44260391]Can I define it as a global since it wasn't working as part of the pointshop item?[/QUOTE] You could; but nothing outside of this script needs it so it's not necessary...
Still getting this [QUOTE]Lua Error: [ERROR] addons/pointshop-master/lua/items/perks/upgrader.lua:62: attempt to index global 'UpgradeTable' (a nil value) 1. fn - addons/pointshop-master/lua/items/perks/upgrader.lua:62 2. unknown - addons/ulib/lua/ulib/shared/hook.lua:183 3. SelectWeapon - [C]:-1 4. DropNotifiedWeapon - gamemodes/terrortown/gamemode/weaponry.lua:224 5. unknown - gamemodes/terrortown/gamemode/weaponry.lua:248 6. unknown - lua/includes/modules/concommand.lua:69[/QUOTE]
Post your code again. I have a feeling that you didn't follow Acecool's suggestion.
Sorry, you need to Log In to post a reply to this thread.