• Explosion Stick
    10 replies, posted
Hey, recently I have started on SWEP's in my continuous (slightly failing :D) attemept to learn lua I am working on a set of 'GodSticks' that will be availible for admins on the server to do a wide vartiety of functions All of the sticks will have the "melee" hold type and with either left or right click will swing like the crowbar and play the "stunstick_swing1" sound or another if stated, such as one that will be a custom sound saying something This stick will be Left Click to create an explosion at your aimpos, with right click to explode yourself and others within a small blast range, so far my code is: cl_init.lua [lua] include ("shared.lua") SWEP.PrintName = "Explosive" SWEP.Slot = 6 SWEP.SlotPos = 3 SWEP.DrawAmmo = false SWEP.DrawCrosshair = true [/lua] init.lua [lua] AddCSLuaFile ("cl_init.lua") AddCSLuaFile ("shared.lua") include ("shared.lua") SWEP.Weight = 5 SWEP.AutoSwitchTo = false SWEP.AutoSwitchFrom = false [/lua] shared.lua [lua] SWEP.Author = "Its Max Blad" SWEP.Purpose = "Explosive GodStick" SWEP.Instructions = "Left Click: Create Explosion \nRight Click: Explode Yourself" SWEP.Category = "GodSticks" SWEP.Spawnable = false SWEP.AdminSpawnable = true SWEP.ViewModel = "models/weapons/v_stunbaton.mdl" SWEP.WorldModel = "models/weapons/w_stunbaton.mdl" SWEP.Primary.ClipSize = -1 SWEP.Primary.DefaultClip = -1 SWEP.Primary.Automatic = false SWEP.Primary.Ammo = "none" SWEP.Secondary.ClipSize = -1 SWEP.Secondary.DefaultClip = -1 SWEP.Secondary.Automatic = false SWEP.Secondary.Ammo = "none" SWEP.Sound = Sound ("stunstick_swing1") SWEP.Delay = 0.6 SWEP.Holdtype = "melee" function SWEP:Initialize() self:SetWeaponHoldType( self.HoldType ) end function SWEP:Deploy() return true end function SWEP:Holster() return true end function SWEP:Think() end function SWEP:PrimaryAttack() if ( !self:CanPrimaryAttack() ) then return end local eyetrace = self.Owner:GetEyeTrace() self.Weapon:EmitSound ( self.Sound ) self.BaseClass.ShootEffects (self) local explode = ents.Create( "env_explosion" ) explode:SetPos( eyetrace.HitPos ) explode:SetOwner( self.Owner ) explode:Spawn() explode:SetKeyValue( "iMagnitude", "220" ) explode:Fire( "Explode", 0, 0 ) explode:EmitSound( "weapon_AWP.Single", 400, 400 ) end function SWEP:SecondaryAttack() if ( !self:CanPrimaryAttack() ) then return end end function SWEP:Reload() return false end [/lua] Obviously I need help which is why im posting here, but also whenever I try to test this when I left or right click it just makes a clicking noise and fails Please help :D
Just a quick glance, but does anything actually set CanPrimaryAttack() to true?
No, but there is nothign to return it false if you CAN fire so I don't think thats it
Does the baseclass return it false by default?
I haven't set a base class for it so it wouldn't even be able to set it false
Just to identify the problem, try taking out line 48 [lua]if ( !self:CanPrimaryAttack() ) then return end[/lua] And see if it works.
... It worked :D So how would I make myself explode then if I right click now please?
Use the exact same code, except the position for the explosion would be pl:GetPos().
Would they not override eachother because they have the same name? [b]EDIT[/b] Just realised it was a local function so it should be fine [editline]27th November 2010[/editline] I tried doing this: [lua] function SWEP:SecondaryAttack() local eyetrace = self.Owner:GetEyeTrace() self.Weapon:EmitSound ( self.Sound ) self.BaseClass.ShootEffects (self) local explode = ents.Create( "env_explosion" ) explode:SetPos( ply:GetPos() ) explode:SetOwner( self.Owner ) explode:Spawn() explode:SetKeyValue( "iMagnitude", "50" ) explode:Fire( "Explode", 0, 0 ) explode:EmitSound( "weapon_AWP.Single", 400, 400 ) end [/lua] But that gave me the error of: "[addons\godstick - explosive\lua\weapons\weapon_godstick_explosive\shared.lua:76] attempt to index global 'ply' (a nil value)"
you want self.Owner:GetPos(), not pl:GetPos(). It doesn't know what ply/pl is.
Ah, I didn't think that ply:GetPos() would be the right one I'll try that other when Ibget back to my computer [b]EDIT[/b] Just tried that and it works great :D Thanks alot for the help everyone!
Sorry, you need to Log In to post a reply to this thread.