• Help implementing Player.TraceHullAttack to a basic SWEP
    1 replies, posted
[b]Objective: [/b]I'm totally new to this and want to create a SWEP that shoots a volume rather than a line. Like a laser type of thing (I know I'd have to add a sprite as well) So I read both the SWEP framework and example: [url]http://wiki.garrysmod.com/?title=SWEP_framework[/url] [url]http://wiki.garrysmod.com/?title=Garry%27s_Example_SWEP[/url] and tried to implement this: [url]http://wiki.garrysmod.com/?title=Player.TraceHullAttack[/url] but it comes up with this: [b]Error: [/b] attempt to call global 'LocalPlayer' (a nil value) Here's my [b]Code:[/b] [lua]function SWEP:PrimaryAttack() local ply = LocalPlayer() if (!SERVER) then return end ply:TraceHullAttack( ply:GetShootPos(), ply:GetAimVector() * 120, Vector( -16, -16, -16), Vector( 36, 36, 36), 100, DMG_AIRBOAT, 200, true) end [/lua] Can anyone help? This is my first attempt to actually make my own SWEP.
You need to use [lua] self.Owner [/lua] Not LocalPlayer(). From the PrimaryAttack() function in Garry's Example SWEP - [lua] function SWEP:PrimaryAttack() local tr = self.Owner:GetEyeTrace() if ( tr.HitWorld ) then return end local effectdata = EffectData() effectdata:SetOrigin( tr.HitPos ) effectdata:SetNormal( tr.HitNormal ) effectdata:SetMagnitude( 8 ) effectdata:SetScale( 1 ) effectdata:SetRadius( 16 ) util.Effect( "Sparks", effectdata ) self.Weapon:EmitSound( ShootSound ) self.BaseClass.ShootEffects( self ) // The rest is only done on the server if ( !SERVER ) then return end // Make a manhack local ent = ents.Create( "npc_manhack" ) ent:SetPos( tr.HitPos + self.Owner:GetAimVector() * -16 ) ent:SetAngles( tr.HitNormal:Angle() ) ent:Spawn() // Weld it to the object that we hit local weld = constraint.Weld( tr.Entity, ent, tr.PhysicsBone, 0, 0 ) undo.Create( "Manhack" ) undo.AddEntity( weld ) undo.AddEntity( ent ) undo.SetPlayer( self.Owner ) undo.Finish() end [/lua]
Sorry, you need to Log In to post a reply to this thread.