SWEP Breaks After Executing Kill Command

Hi. So I just got a little problem with my SWEP I’m trying to make. This is how it works: Skibidi Bop Mm Dada SWEP Showcase - YouTube

Everything was working fine until my friend reported a bug. So if you execute the “kill” command in the console, the swep won’t make an explosion. when the conflict is sus!😳 - YouTube
Seems like the swep can’t find the player location.

The same thing goes for god mode command. If you execute it, it will work the first time, but if you try again, it will break and will no longer work. No errors were seen.

Here’s the code of it. Hope you can fix it for me, I’d be thankful.

AddCSLuaFile()

SWEP.PrintName	= "Skibidi bop mm dada"

SWEP.Author	= "hyperix"
SWEP.Purpose	= "Get Real"

SWEP.Spawnable	= true
SWEP.AdminSpawnable = true
SWEP.AdminOnly = true
SWEP.UseHands	= false
SWEP.DrawAmmo	= false

SWEP.ViewModel	= ""
SWEP.WorldModel	= ""
SWEP.TraceLength = 500
SWEP.ViewModelFOV	= 52
SWEP.Slot			= 0
SWEP.SlotPos		= 5

SWEP.Effect                =  "c4_explosion"
SWEP.EffectAir             =  "c4_explosion_air" 

util.PrecacheModel( SWEP.ViewModel )

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"

local Explosion = {
	"REKT.mp3"
}
local Troll = {
	"dead.mp3"
}
SWEP.DamageDistance = 1000


function SWEP:Initialize()

	self:SetHoldType( "normal" )
	self.CanPerformAttack = true
	timer.Simple(0.05,function()
	end)
	
end

function SWEP:PreDrawViewModel( vm, wep, ply )

	--vm:SetMaterial( "engine/occlusionproxy" ) 
end



if CLIENT then
	SWEP.WepSelectIcon = surface.GetTextureID( "vgui/troll" )
end



function SWEP:PrimaryAttack( right )


	if not self.CanPerformAttack then return false end
	self.CanPerformAttack = false
	self.Owner:SetAnimation(ACT_VICTORY_DANCE)
	
	
	
	local vm = self.Owner:GetViewModel()
	--vm:SendViewModelMatchingSequence( vm:LookupSequence( anim ) )
	self:EmitSound( Sound( "dead.mp3" ))
	--sound.Play(Troll[math.floor(math.Rand(1,3))] , self:GetPos(), 180 , 100, 1 ) 

	timer.Simple(1,function()
		sound.Play("REKT.mp3", self:GetPos())
			local pos = self.Owner:GetPos()
			local tracedata    = {}
				 tracedata.start    = pos
				 tracedata.endpos   = tracedata.start - Vector(0, 0, self.TraceLength)
				 tracedata.filter   = self.Entity
					
			 local trace = util.TraceLine(tracedata)
			 
			 if trace.HitWorld then
				 ParticleEffect(self.Effect,pos,Angle(0,0,0),nil)
			 else 
				 ParticleEffect(self.EffectAir,pos,Angle(0,0,0),nil) 
			 end
		if CLIENT then
			sound.Play("REKT.mp3", self:GetPos())
		elseif SERVER then
		  sound.Play("REKT.mp3", self:GetPos())
			util.BlastDamage(self, self.Owner, pos, self.DamageDistance, 250)
			for k,v in pairs(ents.FindInSphere(self:GetPos(),1024)) do
				if v and v:IsValid() then
					if not type(v) == "prop_physics" then return false end
					constraint.RemoveConstraints( v, "Weld" ) 
					local phys = v:GetPhysicsObject()
					if phys and phys:IsValid() then
						phys:Wake()
						phys:EnableMotion(true)
						phys:ApplyForceCenter(-(self:GetPos()-phys:GetPos()) * (phys:GetMass()*(1024-phys:GetPos():Distance(self:GetPos())) ) )
					end
				end
			end
		end
	end)
end
function SWEP:SecondaryAttack()

end
function SWEP:OnRemove()
	
	if ( IsValid( self.Owner ) && CLIENT && self.Owner:IsPlayer() ) then
		local vm = self.Owner:GetViewModel()
		if ( IsValid( vm ) ) then vm:SetMaterial( "" ) end
	end
	
end

function SWEP:Holster( wep )

	self:OnRemove()

	return true

end

function SWEP:Deploy()


	
	return true

end

function SWEP:Think()
	
	
	
end

Many things:

  • For ents.Find, ents.GetAll, player.GetAll and many other functions, you can use the ipairs iterator (ipairs - Garry's Mod Wiki) to improve loop performance.
  • Also, you don’t need to check if “v” is valid because all entities output from these functions will ALWAYS be valid (only in the same tick). So you can delete this line.
  • Finally, if you need to check the type of a data, it will never return “prop_physics”, but only the name of the type (bool, number, string), more information here : type - Garry's Mod Wiki

Just try to do this kind of cleaning in your code and you can see if there are any other problems.

1 Like

So basically I need to remove the second line and change props_physics to something else?

Honestly, you don’t even need this line.

but will it fix the problem?

Not really because you have to use this hook: GM:PlayerDeath - Garry's Mod Wiki to be able to execute code when the player dies. I don’t think there is a way through a weapon to handle the player’s death. The issues I have listed above will allow you to make the code for your weapon more… effective and simple.