Author Topic: Weapon Script [Need Help]  (Read 4425 times)

0 Members and 1 Guest are viewing this topic.

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
Weapon Script [Need Help]
« on: July 27, 2011, 10:13:20 AM »
Code: [Select]
if (SERVER) then
AddCSLuaFile ("shared.lua");
end

if (CLIENT) then
SWEP.PrintName = "EXD2";
SWEP.Slot = 2;
SWEP.SlotPos = 4;
SWEP.DrawAmmo = true;
SWEP.DrawCrosshair = true;
end

SWEP.Author = "HeLLFox_15"
SWEP.Contact = "youremail@gmail.com"
SWEP.Purpose = "Blow peaple up."
SWEP.Instructions = "Primary to fire a bullet, Secondary to make an explosion where you are pointing"
SWEP.Category = "HeLLFox_15-SWEPS"
 
SWEP.Spawnable = false;
SWEP.AdminSpawnable = true;
 
SWEP.ViewModel = "models/weapons/v_357.mdl";
SWEP.WorldModel = "models/weapons/w_357.mdl";

SWEP.Weight = 5;
SWEP.AutoSwitchTo = false;
SWEP.AutoSwitchFrom = false;
 
SWEP.Primary.ClipSize = 8;
SWEP.Primary.DefaultClip = 20;
SWEP.Primary.Automatic = false;
SWEP.Primary.Ammo = "357";
 
SWEP.Secondary.ClipSize = -1;
SWEP.Secondary.DefaultClip = -1;
SWEP.Secondary.Automatic = false;
SWEP.Secondary.Ammo = "none";
 
SWEP.Sound = Sound ("weapon_357.Single")
SWEP.Damage = 50
SWEP.Spread = 0.02
SWEP.NumBul = 1
SWEP.Delay = 0.6
SWEP.Force = 3
 
function SWEP:Deploy()
return true
end
 
function SWEP:Holster()
return true
end
 
function SWEP:Think()
end
 
function SWEP:SecondaryAttack() // when secondary attack happens
 
// Make sure we can shoot first
if ( !self:CanPrimaryAttack() ) then return end
 
local eyetrace = self.Owner:GetEyeTrace();
// this gets where you are looking. The SWep is making an explosion where you are LOOKING, right?
 
self.Weapon:EmitSound ( self.Sound )
// this makes the sound, which I specified earlier in the code
 
self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
//this makes the shooting animation for the 357
 
local explodea = ents.Create("env_ar2explosion")
explodea:SetPos( eyetrace.HitPos )
explodea:SetOwner( self.Owner )
explodea:Spawn()
explodea:SetKeyValue("iMagnitude","575")
explodea:Fire("Explode", 0, 0 )
explodea:EmitSound("weapon_AWP.Single", 400, 400 )

for _,trga in pairs( ents.FindInSphere(eyetrace.HitPos,258) ) do
if( trga:IsValid() ) then

constraint.RemoveAll(trga)
trga:PhysWake()

if( trga:IsPlayer() and trga ~= self.Owner ) then
if( ( trga:Health() - 50 ) <= 0 ) then
trga:Kill()
else
trga:SetHealth( trga:Health() - 50 )
end
end

trga:SetVelocity( ( trga:GetPos() - eyetrace.HitPos ) * 9000 )

local phys = trga:GetPhysicsObject()
local vecforceapl = ( trga:GetPos() - eyetrace.HitPos ) * ( 9000 * phys:GetMass() )

constraint.RemoveAll(trga)
phys:EnableMotion(true)
phys:ApplyForceCenter( vecforceapl )
phys:AddVelocity( vecforceapl )

end
end
 
self.Weapon:SetNextPrimaryFire( CurTime() + self.Delay )
self.Weapon:SetNextSecondaryFire( CurTime() + self.Delay )
//this sets the delay for the next primary and secondary fires.
 
self:TakePrimaryAmmo(1) //removes 1 ammo from our clip
 
end //telling Gmod that it's the end of the function
 
function SWEP:PrimaryAttack() //when +attack1 happens
 
// Make sure we can shoot first
if ( !self:CanPrimaryAttack() ) then return end
 
local bullet = {} //creates a table for the properties of the bullet
bullet.Num = self.NumBul //number of bullets you are shooting
bullet.Src = self.Owner:GetShootPos() // Source, where you are standing
bullet.Dir = self.Owner:GetAimVector() // direction of bullet, where you are looking
bullet.Spread = Vector( self.Spread, self.Spread, 0 ) // spread of bullet, how accurate it is
bullet.Tracer = 0 // this is the beam behind the bullet.
bullet.Force = 99999999 // how powerful it is
bullet.Damage = 99999999 //how much damage it does to people
bullet.AmmoType = self.Primary.Ammo //what type of ammo you are using
 
self.Owner:FireBullets( bullet ) //actually shoots the bullet.
 
self.Weapon:EmitSound ( self.Sound )
// this makes the sound, which I specified earlier in the code
 
self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
//this makes the shooting animation for the 357
 
self.Weapon:SetNextPrimaryFire( CurTime() + self.Delay )
self.Weapon:SetNextSecondaryFire( CurTime() + self.Delay )
//this sets the delay for the next primary and secondary fires.
 
self:TakePrimaryAmmo(1) //removes 1 ammo from our clip
end //end our function
 
function SWEP:Reload()
self.Weapon:DefaultReload( ACT_VM_RELOAD ) //animation for reloading
end

The first problem is that some times it makes props fly out of the map ( or breaks world entities ), and that creates this server crashing error...

"This does not give any errors into the console just crashes instantly"

The second one is this...

Code: [Select]
Can't find factory for entity: env_ar2explosion
[@weapons\weapon_exd2\shared.lua:72] Tried to use a NULL entity!
Can't find factory for entity: env_ar2explosion
[@weapons\weapon_exd2\shared.lua:72] Tried to use a NULL entity!
Can't find factory for entity: env_ar2explosion
[@weapons\weapon_exd2\shared.lua:72] Tried to use a NULL entity!

And the third is this...

Code: [Select]
L 07/27/2011 - 12:06:32: Log file started (file "logs\L0727002.log") (game "C:\srcds\orangebox\garrysmod") (version "4616")
L 07/27/2011 - 12:08:04: "HeLLFox_15<2><STEAM_0:1:20305110><>" connected, address "99.237.202.61:27005"
L 07/27/2011 - 12:08:04: "HeLLFox_15<2><STEAM_0:1:20305110><>" STEAM USERID validated
L 07/27/2011 - 12:08:05: server_cvar: "sv_tags" "alltalk,garrysmod125,gm:sandbox,gravhull,scriptenforcer,wiresvn2463"
L 07/27/2011 - 12:08:50: "HeLLFox_15<2><STEAM_0:1:20305110><>" entered the game
L 07/27/2011 - 12:10:09: Lua Error: [@addons\hellfox_15-sweps\lua\weapons\weapon_exd2\shared.lua:96] Tried to use invalid object (type IPhysicsObject) (Object was NULL or not of the right type)
L 07/27/2011 - 12:10:43: Log file closed

The weapon works and does what it is supposed to ( except the server crashing bit ), but I would like it to not crash servers and to give less or no errors.

Please and thank you.
I cry every time I see that I am not a respected member of this community.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Weapon Script [Need Help]
« Reply #1 on: July 27, 2011, 02:52:27 PM »
The second problem is that "env_ar2explosion" isn't a valid entity classname (according to the Lua error, anyways).
Experiencing God's grace one day at a time.

Offline LuaTenshi

  • Hero Member
  • *****
  • Posts: 545
  • Karma: 47
  • Just your ordinary moon angel!
    • Mirai.Red
Re: Weapon Script [Need Help]
« Reply #2 on: July 28, 2011, 07:13:24 AM »
The second problem is that "env_ar2explosion" isn't a valid entity classname (according to the Lua error, anyways).

Well then that's strange, it creates the "env_ar2explosion" any way, but it does get the error.
I cry every time I see that I am not a respected member of this community.

Offline skullorz

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Re: Weapon Script [Need Help]
« Reply #3 on: November 12, 2011, 12:10:33 PM »
env_ar2explosion is a serverside entity, put the entity creation in an if SERVER then statement.

EDIT : Ban me for necroposting.
« Last Edit: November 12, 2011, 12:12:11 PM by skullorz »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Weapon Script [Need Help]
« Reply #4 on: November 12, 2011, 12:55:25 PM »
EDIT : Ban me for necroposting.

Hi, welcome to Ulysses.
We aren't facepunch. We actually appreciate help on, yes, even old posts that may not have been answered.
Posting just to post, not so much, but, your information may be helpful.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Weapon Script [Need Help]
« Reply #5 on: November 12, 2011, 04:07:44 PM »
Expanding on Jam's reasoning a little bit.


We encourage people to use Ulysses as a resource. Even if it's non ULX related... so long as we keep non ULX issues in the appropriate forums.

We also encourage people performing searches before asking questions because chances are the problem has been address at some point in the past.

There is nothing more annoying than searching and finding someone with the exact (or very similar) problem as you just to see that no one ever helped them with it.

So please, find all the old posts that have gone unanswered that you wish, and answer them to your hearts content. So long as you are adding useful content to the posts, there is no expiration to the posts.