Need help with Slam script for TTT

I have this slam script here, which worked fine before, until i tried to edit it so i can have 2 slams instead of just 1. When i use it like this, it shows me i have 2 slams. I can set 1 up and have 1 left, but when i want to set the last one, it just uses it without placing a slam.
I’m new to lua so i don’t know much.
Can someone help me, so that i can have 2 slams, which i can actually place?


if SERVER then
	AddCSLuaFile("shared.lua")
end

SWEP.HoldType			= "slam"

if CLIENT then
   SWEP.PrintName			= "S.L.A.M."
   SWEP.Slot				= 6

   SWEP.ViewModelFOV = 65
   
   SWEP.EquipMenuData = {
      type  = "item_weapon",
      name  = "S.L.A.M.",
      desc  = "Laser tripmine."
   };

   SWEP.Icon = "VGUI/ttt/slam"
end

SWEP.Base = "weapon_tttbase"

SWEP.Kind = WEAPON_EQUIP
SWEP.CanBuy = {ROLE_TRAITOR}
SWEP.WeaponID = AMMO_SLAM

SWEP.UseHands      = true
SWEP.ViewModelFlip    = false
SWEP.ViewModelFOV    = 60
SWEP.ViewModel      = "models/weapons/c_slam.mdl"
SWEP.WorldModel = Model("models/weapons/w_slam.mdl")

SWEP.DrawCrosshair      = false
SWEP.ViewModelFlip      = false
SWEP.Primary.ClipSize       = 2
SWEP.Primary.DefaultClip    = 2
SWEP.Primary.Automatic      = false
SWEP.Primary.Ammo       = "none"
SWEP.Primary.Delay = 0.1


SWEP.NoSights = true

local sticksound = Sound( "weapons/slam/mine_mode.wav" )

function SWEP:Deploy()
	self.Weapon:SendWeaponAnim(ACT_SLAM_TRIPMINE_DRAW)
	return true
end

function SWEP:PrimaryAttack()
   self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
   self:StickSLAM()
end

function SWEP:SecondaryAttack()
	return false
end

function SWEP:StickSLAM()
	if SERVER then
	self:TakePrimaryAmmo( 1 )
		local ply = self.Owner
		if not IsValid(ply) then return end
		 if self.Planted then return end
		 
		 local ignore = {ply, self.Weapon}
		 local spos = ply:GetShootPos()
		 local epos = spos + ply:GetAimVector() * 120
		 local tr = util.TraceLine({start=spos, endpos=epos, filter=ignore, mask=MASK_SOLID})
		 if tr.HitWorld then
			local slam = ents.Create("ttt_slam_trip")
			if IsValid(slam) then
				slam:PointAtEntity(ply)
				
				local tr_ent = util.TraceEntity({start=spos, endpos=epos, filter=ignore, mask=MASK_SOLID}, slam)
				if tr_ent.HitWorld then
					self.Weapon:SendWeaponAnim(ACT_SLAM_TRIPMINE_ATTACH)
					local holdup = self.Owner:GetViewModel():SequenceDuration()
                               
					timer.Simple(holdup,function()
						if SERVER then
							self:SendWeaponAnim( ACT_SLAM_TRIPMINE_ATTACH2 )
							end    
					end)
					
								
					self:SendWeaponAnim( ACT_SLAM_TRIPMINE_ATTACH )
                               
					local holdup = self.Owner:GetViewModel():SequenceDuration()
                               
					timer.Simple(holdup,
					function()
					if SERVER then
						self:SendWeaponAnim( ACT_SLAM_TRIPMINE_ATTACH2 )
						end    
					end)
                                       
					timer.Simple(holdup + .1,
					function()
						if SERVER then
							if self.Owner == nil then return end
							if self.Weapon:Clip1() == 0 && self.Owner:GetAmmoCount( self.Weapon:GetPrimaryAmmoType() ) == 0 then
							self:Remove()
							else
							self:Deploy()
							end
						end
					end)                       

					self.Planted = true
					timer.Simple(holdup + 0.1, function()
						if not IsValid(self) then return end
					
						local ang = tr_ent.HitNormal:Angle()
						ang:RotateAroundAxis(ang:Right(), -90)

						slam:SetPos(tr_ent.HitPos + tr_ent.HitNormal * 2)
						slam:SetAngles(ang)
						slam:SetPlacer(ply)
						slam:Spawn()
						
						slam.fingerprints = { ply }
						
						local phys = slam:GetPhysicsObject()
						if IsValid(phys) then
							phys:EnableMotion(false)
						end
						
						self:EmitSound(sticksound)
						
						slam.IsOnWall = true
						if SERVER then
								if self.Owner == nil then return end
								if self.Weapon:Clip1() == 0 && self.Owner:GetAmmoCount( self.Weapon:GetPrimaryAmmoType() ) == 0 then
								self:Remove()
								else
								self:Deploy()
								end
							end
						--self.Owner:GetAmmoCount( self.Weapon:GetPrimaryAmmoType() )
						--self:Remove()
						self.Planted = true
						
					end)
				end
			end
			
			ply:SetAnimation( PLAYER_ATTACK1 )
		 end
	end
end

function SWEP:Reload()
   return false
end

function SWEP:OnRemove()
   if CLIENT and IsValid(self.Owner) and self.Owner == LocalPlayer() and self.Owner:Alive() then
      RunConsoleCommand("lastinv")
   end
end


if SERVER then
   resource.AddFile("materials/VGUI/ttt/slam.vmt")
end

Thanks in advance!

I cant see why you would remove that ammo before you have checked if it’s placed first.

Firstly copy this line then remove it.


self:TakePrimaryAmmo( 1 )

Then place it after this.


self.Planted = true

Facepunch really needs line number when using code tags arggh…

Any way try that it should work fine.

I changed it like you said, but now it looks like this:

Slam 2 charges
Place 1
Slam 1 charge
Can’t place the last one

It doesn’t want to let me place the last Slam.

Edit: I just deleted the line:


if self.Planted then return end

It let’s me set up the slam correctly now, only the slam counter is wrong. It shows i have 1, but counts 0 as well. So overall i can place 2 slams now.

One problem is still there: When i pick 1 slam up i can place 2 new ones.

Confused about what you said. But you do understand most programming languages count Zero as a number 0-1 computers don’t start from 1 then go up they start at zero.

I do understand, i just wanted to exclude 0.
I’ve rewrote some stuff (now it places 2 slams and is done, the slam counter is correct as well) and now it’s working almost like i wanted it to be.


if SERVER then
   AddCSLuaFile( "shared.lua" )
end

if CLIENT then
   SWEP.PrintName = "S.L.A.M."
   SWEP.Slot = 6
   SWEP.Icon = "VGUI/ttt/slam"
end

-- Always derive from weapon_tttbase
SWEP.Base = "weapon_tttbase"

-- Standard GMod values
SWEP.HoldType = "slam"

SWEP.Primary.Ammo = "none"
SWEP.Primary.Delay = 0.1
SWEP.Primary.Automatic = false
SWEP.Primary.ClipSize = 2
SWEP.Primary.DefaultClip = 2
--SWEP.FiresUnderwater = false

-- Model settings
SWEP.UseHands = true
SWEP.ViewModelFlip = false
SWEP.ViewModelFOV = 65
SWEP.ViewModel      = "models/weapons/c_slam.mdl"
SWEP.WorldModel = Model("models/weapons/w_slam.mdl")

local sticksound = Sound( "weapons/slam/mine_mode.wav" )

--- TTT config values

-- Kind specifies the category this weapon is in. Players can only carry one of
-- each. Can be: WEAPON_... MELEE, PISTOL, HEAVY, NADE, CARRY, EQUIP1, EQUIP2 or ROLE.
-- Matching SWEP.Slot values: 0      1       2     3      4      6       7        8
SWEP.Kind = WEAPON_EQUIP2
   
-- If AutoSpawnable is true and SWEP.Kind is not WEAPON_EQUIP1/2, then this gun can
-- be spawned as a random weapon.
SWEP.AutoSpawnable = false

-- CanBuy is a table of ROLE_* entries like ROLE_TRAITOR and ROLE_DETECTIVE. If
-- a role is in this table, those players can buy this.
SWEP.CanBuy = { ROLE_TRAITOR }

-- InLoadoutFor is a table of ROLE_* entries that specifies which roles should
-- receive this weapon as soon as the round starts. In this case, none.
SWEP.InLoadoutFor = { nil }

-- If LimitedStock is true, you can only buy one per round.
SWEP.LimitedStock = false

-- If AllowDrop is false, players can't manually drop the gun with Q
SWEP.AllowDrop = false

-- If NoSights is true, the weapon won't have ironsights
SWEP.NoSights = true

-- Equipment menu information is only needed on the client
if CLIENT then
   SWEP.EquipMenuData = {
      type = "item_weapon",
      desc = "Laser tripmine."
   };
end

function SWEP:Deploy()
    self:SendWeaponAnim( ACT_SLAM_TRIPMINE_DRAW )
    return true
end 
     
function SWEP:OnRemove()
    if CLIENT and IsValid(self.Owner) and self.Owner == LocalPlayer() and self.Owner:Alive() then
      RunConsoleCommand("lastinv")
    end
end
     
function SWEP:PrimaryAttack()
	self:StickSLAM()
	self.Weapon:SetNextPrimaryFire(CurTime()+(self.Primary.Delay))
end
     
function SWEP:StickSLAM()
 if SERVER then
      local ply = self.Owner
      if not IsValid(ply) then return end
 
 
      local ignore = {ply, self.Weapon}
      local spos = ply:GetShootPos()
      local epos = spos + ply:GetAimVector() * 180
      local tr = util.TraceLine({start=spos, endpos=epos, filter=ignore, mask=MASK_SOLID})
 
      if tr.HitWorld then
         local slam = ents.Create("ttt_slam_trip")
         if IsValid(slam) then
			slam:PointAtEntity(ply)
 
            local tr_ent = util.TraceEntity({start=spos, endpos=epos, filter=ignore, mask=MASK_SOLID}, slam)
 
            if tr_ent.HitWorld then
				timer.Simple(0.1, function()
						if not IsValid(self) then return end
					
						local ang = tr_ent.HitNormal:Angle()
						ang:RotateAroundAxis(ang:Right(), -90)

						slam:SetPos(tr_ent.HitPos + tr_ent.HitNormal * 2)
						slam:SetAngles(ang)
						slam:SetPlacer(ply)
						slam:Spawn()
						
						slam.fingerprints = { ply }
						
	
						local phys = slam:GetPhysicsObject()
						if IsValid(phys) then
							phys:EnableMotion(false)
						end
						
						self:EmitSound(sticksound)
								
                                self:SendWeaponAnim( ACT_SLAM_TRIPMINE_ATTACH )
                               
                                local holdup = self.Owner:GetViewModel():SequenceDuration()
                               
                                timer.Simple(holdup,
                                function()
                                if SERVER then
                                        self:SendWeaponAnim( ACT_SLAM_TRIPMINE_ATTACH2 )
                                end    
                                end)
                                       
                                timer.Simple(holdup + .1,
                                function()
                                        if SERVER then
                                                if self.Owner == nil then return end
                                                if self.Weapon:Clip1() == 0 && self.Owner:GetAmmoCount( self.Weapon:GetPrimaryAmmoType() ) == 0 then
												self:Remove()
                                                else
                                                self:Deploy()
                                                end
                                        end
                                end)                       

                                self.Planted = true
								self:TakePrimaryAmmo( 1 )
                               
				end)		 
			end
		end
	end
end

end

function SWEP:SecondaryAttack()
    return false
end  

function SWEP:Reload()
   return false
end

if SERVER then
   resource.AddFile("materials/VGUI/ttt/slam.vmt")
   resource.AddFile("weapons/slam/mine_mode.wav")
end

There is only 1 problem left. I made it so you can pick your Slam back up, but when i pick 1 up i get 2 slams again instead of just the one. Basically i can duplicate the Slams by picking them up again, which i don’t want of course.
Here is the code maybe you know an answer to that.


function ENT:UseOverride(activator)
	if not IsValid(self) or self:GetDisarmed() then return end
	
   if IsValid(activator) and activator:IsPlayer() and not self.Exploding then
      local owner = self:GetPlacer() 
      if owner == activator then
			if (not IsValid(activator)) or (not activator:IsTerror()) or (not activator:Alive()) then return end
			
			if not activator:CanCarryType(WEAPON_EQUIP1) then
				LANG.Msg(activator, "You cannot carry this S.L.A.M.")
			else
				local wep = activator:Give("weapon_ttt_slam")
				if IsValid(wep) then
				   wep.fingerprints = wep.fingerprints or {}
				   table.Add(wep.fingerprints, prints)

				    if self.beam != nil then
						self.beam:Remove()
					end
					if self.beamEnd != nil then
						self.beamEnd:Remove()
					end
					self:Remove()
				end
			end
      end
   end
end