Need help Fixing weapon for TTT


if SERVER then
    AddCSLuaFile("shared.lua")
  
     
	 
	 
elseif CLIENT then
    SWEP.PrintName          = "Cat Launcher"
    SWEP.Slot               = 6
    SWEP.SlotPos            = 3
    SWEP.DrawCrosshair = false
     
    SWEP.EquipMenuData = {
      type = "Strange thing",
      desc = "Hope You Don't Like 
 Cats."
   }
    
   SWEP.Icon = "vgui/ttt/icon_headcrab.png"
end

// Original 'Bazooka' script by ZeroPoint

SWEP.Author			= "Keonesan"
SWEP.Contact		= "keonesan@sincitymoto.com"
SWEP.Purpose		= "I hope you don't like cats..."
SWEP.Instructions	      = "Left Click To Fire"
SWEP.Category 		= "Keonesan's Weapons 09"

SWEP.ViewModelFOV	= 62
SWEP.ViewModelFlip	= false
SWEP.ViewModel		= "models/weapons/v_crossbow.mdl"
SWEP.WorldModel		= "models/weapons/w_crossbow.mdl"
SWEP.AnimPrefix		= "rpg"

SWEP.Spawnable			= true
SWEP.AdminSpawnable		= true
SWEP.Base = "weapon_tttbase"
SWEP.Sound = Sound ("weapons/cat_launcher2.wav")
SWEP.Primary.ClipSize		= 1
SWEP.Primary.DefaultClip	= 9999
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "rpg_round"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo		= "none"
SWEP.Kind = WEAPON_EQUIP
SWEP.CanBuy = {ROLE_TRAITOR}
SWEP.LimitedStock = true
function SWEP:Deploy()
	self.Weapon:EmitSound ("weapons/cat_launcher1.wav");
	return true
end

function SWEP:Initialize()
end

function SWEP:Precache()
	util.PrecacheSound("weapons/cat_launcher1.wav")
	util.PrecacheSound("Buttons.snd14")
end


function SWEP:PrimaryAttack()
	if self:CanPrimaryAttack() then
		self:FireRocket()
		self.Weapon:EmitSound ( self.Sound )
		self.Weapon:TakePrimaryAmmo(1)
		
		self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
		self.Owner:MuzzleFlash()
		self.Owner:SetAnimation( PLAYER_ATTACK1 )
		if self:Clip1() == 0 then
			self:Reload()
		end
	else
		self.Weapon:EmitSound("Buttons.snd14")
	end
	self.Weapon:SetNextPrimaryFire(CurTime()+3)
end

function SWEP:FireRocket()
	local aim = self.Owner:GetAimVector()
	local side = aim:Cross(Vector(0,0,1))
	local up = side:Cross(aim)
	local pos = self.Owner:GetShootPos() +  aim * 24 + side * 8 + up * -1
	local Rocket = ents.Create("cat")
		if !Rocket:IsValid() then return false end
		Rocket:SetAngles(aim:Angle())
		Rocket:SetPos(pos)
	Rocket:SetOwner(self.Owner)
	Rocket:Spawn()
	Rocket:Activate()
	Rocket:SetVelocity(Rocket:GetForward()*1500)
end

function SWEP:SecondaryAttack()
	self.Weapon:EmitSound ("weapons/cat_launcher3.wav");
	return false
end

function SWEP:Reload()
	local tr = self.Owner:GetEyeTrace()
	if tr.Fraction < 0.0044 and tr.Entity:IsValid() and tr.Entity:GetClass() == "munition_crate" then
		if CurTime() < (self.NextReload or 0) then return false end
		self.NextReload = CurTime() + 3
		if SERVER then
			local availammo = RD_GetResourceAmount(tr.Entity, "Munitions")
			if availammo > 0 then
				local addammo = math.min(math.Clamp(9 - self.Owner:GetAmmoCount("rpg_round"), 0, 3), math.floor(availammo/200))
				if addammo > 0 then
					RD_ConsumeResource(tr.Entity, "Munitions", addammo*200)
					self.Owner:GiveAmmo(addammo, "rpg_round")
				end
			end
		end
	else
		self.Weapon:DefaultReload( ACT_VM_RELOAD );
	end
end


getting these lua errors

[ERROR] addons/adam west cat launcher/lua/weapons/cat_launcher_v2.1/shared.lua:88: attempt to call field ‘Create’ (a nil value)

  1. FireRocket - addons/adam west cat launcher/lua/weapons/cat_launcher_v2.1/shared.lua:88
  2. unknown - addons/adam west cat launcher/lua/weapons/cat_launcher_v2.1/shared.lua:67

Any clue on how to fix this?

[editline]22nd November 2014[/editline]

Also getting

ERROR: Trying to derive entity cat from non existant entity base_gmodentity!

ents.Create doesn’t exist on the CLIENT, make sure it is only executing on the SERVER.

Your entity is referencing a base / sandbox entity which wasn’t loaded ( use DeriveGamemode to include the files )

I’m not sure how to do this.

To have code only run on one realm which is normally executed on both ( such as SWEP:PrimaryAttack and other SWEP functions ), use this:


if ( SERVER ) then
	local _ent = ents.Create( "blah" );
	_ent:SetPos( vector_origin );
	_ent:SetAngles( angle_zero );
	_ent:Spawn( );
	_ent:Activate( );
end

NOTE: If you do need important client code to run in a SWEP, make sure you do the same thing as above but use IsFirstTimePredicted( ) in place of SERVER, and clientside code in place of the _ent stuff.

I’m not sure great with this stuff, how do I make it client side code to replace the _ent stuff?

Here are a few examples that should help:

https://dl.dropboxusercontent.com/u/26074909/tutoring/loading_files_across_realms.lua.html
https://dl.dropboxusercontent.com/u/26074909/tutoring/tables/quick_intro_to_tables.lua.html
https://dl.dropboxusercontent.com/u/26074909/tutoring/logic/ternary_operations.lua.html
https://dl.dropboxusercontent.com/u/26074909/tutoring/strings/string_concatenation.lua.html

That honestly confused me more.

[editline]22nd November 2014[/editline]

Still no clue. I’m still learning this stuff.

Who coded the SWEP?

So, instead of:


if ( SERVER ) then
	local _ent = ents.Create( "blah" );
	_ent:SetPos( vector_origin );
	_ent:SetAngles( angle_zero );
	_ent:Spawn( );
	_ent:Activate( );
end

it would be


if ( CLIENT ) then
	...
end

Where … is removed / replaced with different code.

This would be the same if you needed client and server code:


if ( SERVER ) then
	local _ent = ents.Create( "blah" );
	_ent:SetPos( vector_origin );
	_ent:SetAngles( angle_zero );
	_ent:Spawn( );
	_ent:Activate( );
else
	...
end

This is the same as the above code:


if ( CLIENT ) then
	...
else
	local _ent = ents.Create( "blah" );
	_ent:SetPos( vector_origin );
	_ent:SetAngles( angle_zero );
	_ent:Spawn( );
	_ent:Activate( );
end

And this would be the same:


if ( SERVER ) then
	local _ent = ents.Create( "blah" );
	_ent:SetPos( vector_origin );
	_ent:SetAngles( angle_zero );
	_ent:Spawn( );
	_ent:Activate( );
elseif ( CLIENT ) then
	...
end

Basically, the examples just show different ways of doing the same thing for the different topic categories ( the links, and now the if statements above )…

so basically


if ( SERVER ) then
	local _ent = ents.Create( "cat" );
	_ent:SetPos( vector_origin );
	_ent:SetAngles( angle_zero );
	_ent:Spawn( );
	_ent:Activate( );
elseif ( CLIENT ) then
	local _ent = ents.Create( "cat" );
	_ent:SetPos( vector_origin );
	_ent:SetAngles( angle_zero );
	_ent:Spawn( );
	_ent:Activate( );
end

And I put this in the shared.lua of the weapon, not the shared.lua of the entity?

Not quite; read up. ents.Create doesn’t exist in the CLIENT realm, meaning that code inside the elseif ( CLIENT ) then … end segment will give you the same error you’re having.

You just needed to fix the entity being created on client as the issue, so you can leave the code as:


if ( SERVER ) then
	local _ent = ents.Create( "cat" );
	_ent:SetPos( vector_origin );
	_ent:SetAngles( angle_zero );
	_ent:Spawn( );
	_ent:Activate( );
end

And put it in your FireRocket function… HOWEVER, you can’t just put that code in because it won’t help you. Your ents.Create setup is different and the code above was meant to serve as an EXAMPLE of where to add the if ( SERVER ) then code and where to add the end instead of copying and pasting… There is different data being used, so you’d simply add if ( SERVER ) then before local Rocket = … and you would insert an end after Rocket:SetVelocity…:


function SWEP:FireRocket()
	local aim = self.Owner:GetAimVector()
	local side = aim:Cross(Vector(0,0,1))
	local up = side:Cross(aim)
	local pos = self.Owner:GetShootPos() +  aim * 24 + side * 8 + up * -1
	local Rocket = ents.Create("cat")
		if !Rocket:IsValid() then return false end
		Rocket:SetAngles(aim:Angle())
		Rocket:SetPos(pos)
	Rocket:SetOwner(self.Owner)
	Rocket:Spawn()
	Rocket:Activate()
	Rocket:SetVelocity(Rocket:GetForward()*1500)
end

to make it look like this


function SWEP:FireRocket()
	local aim = self.Owner:GetAimVector()
	local side = aim:Cross(Vector(0,0,1))
	local up = side:Cross(aim)
	local pos = self.Owner:GetShootPos() +  aim * 24 + side * 8 + up * -1

	if ( SERVER ) then
		local Rocket = ents.Create("cat")
		if !Rocket:IsValid() then return false end
		Rocket:SetAngles(aim:Angle())
		Rocket:SetPos(pos)
		Rocket:SetOwner(self.Owner)
		Rocket:Spawn()
		Rocket:Activate()
		Rocket:SetVelocity(Rocket:GetForward()*1500)
	end
end

I’d also recommend you change if !Rocket:IsValid( ) … to if ( !IsValid( Rocket ) ) … because if Rocket isn’t valid ( ie didn’t spawn properly ) then that line will error saying IsValid is nil because Rocket.IsValid won’t exist because Rocket wasn’t spawned.

Then it looks like this ( I changed it to make it a little bit more readable by adding air and semi-colons ).


function SWEP:FireRocket( )
	// ents.Create only exists on the server, ensure it is only created on the server:
	if ( SERVER ) then
		local Rocket = ents.Create( "cat" );

		// The entity didn't spawn correctly, this could be due to entity limit being reached, the entity "cat" not existing, etc...
		if ( !IsValid( Rocket ) ) then return false; end

		// These vars aren't used anywhere EXCEPT for the Rocket, so these should only be declared if the rocket spawned correctly...
		local aim = self.Owner:GetAimVector( );
		local side = aim:Cross( Vector( 0, 0, 1 ) );
		local up = side:Cross( aim );
		local pos = self.Owner:GetShootPos( ) +  aim * 24 + side * 8 + up * -1;

		// Rocket did spawn, go ahead and set up the vars..
		Rocket:SetAngles( aim:Angle( ) );
		Rocket:SetPos( pos );
		Rocket:SetOwner( self.Owner );
		Rocket:Spawn( );
		Rocket:Activate( );
		Rocket:SetVelocity( Rocket:GetForward( ) * 1500 );
	end
end

Hopefully that helps clarify things. I added a few comments too.

so just put


function SWEP:FireRocket( )
	// ents.Create only exists on the server, ensure it is only created on the server:
	if ( SERVER ) then
		local Rocket = ents.Create( "cat" );

		// The entity didn't spawn correctly, this could be due to entity limit being reached, the entity "cat" not existing, etc...
		if ( !IsValid( Rocket ) ) then return false; end

		// These vars aren't used anywhere EXCEPT for the Rocket, so these should only be declared if the rocket spawned correctly...
		local aim = self.Owner:GetAimVector( );
		local side = aim:Cross( Vector( 0, 0, 1 ) );
		local up = side:Cross( aim );
		local pos = self.Owner:GetShootPos( ) +  aim * 24 + side * 8 + up * -1;

		// Rocket did spawn, go ahead and set up the vars..
		Rocket:SetAngles( aim:Angle( ) );
		Rocket:SetPos( pos );
		Rocket:SetOwner( self.Owner );
		Rocket:Spawn( );
		Rocket:Activate( );
		Rocket:SetVelocity( Rocket:GetForward( ) * 1500 );
	end
end

into the FireRocket function and it’ll work?

Unless there are other errors, yes…

I’d also recommend taking a look at some of these:

Hey, welcome to FacePunch.
This forum is for devs that need help working on things. Here are some resources to help you get started:

Generalized Lua Help ( Links to Wikis, Answers the question of “Where do I post a simple question or DarkRP Specific question”, links to other resources compiled by forum members )
https://dl.dropboxusercontent.com/u/26074909/tutoring/___welcome_docs/_welcome_general_lua_learning.lua.html

Useful Programs ( SteamCMD, Autosizer, Desktops, Process Explorer ) and Notepad++ Upgrades
https://dl.dropboxusercontent.com/u/26074909/tutoring/___welcome_docs/_welcome_useful_programs_and_notepadpp_upgrades.lua.html