SWEP calling an entity creation causing crash

If anyone’s seen the latest few posts in the WAYWO thread you may have seen a bit of the CTF gamemode I’m currently working on. In one of the recent updates I worked on I created flag SWEPs instead of parenting the flag to the player.

The following is the two bits of code that control dropping the flag:
[lua]
– shared.lua SWEP code
function SWEP:Holster()
if SERVER then
GAMEMODE:SpawnFlag(“red_flag”,self.Owner:GetPos(),nil,true)
self.Owner:StripWeapon(self:GetClass())

	--net message for dropping flag announcement
end

end

– init.lua GAMEMODE code
function GM:SpawnFlag(flag,pos,ang,ret)
– make sure no flags are on the map already by removing them
for k,v in pairs(ents.FindByClass(flag)) do
v:Remove()
end

-- check to make sure the map is in the authorized map list
if table.HasValue(GAMEMODE.Maps,game.GetMap()) then
	local flagTbl
	if flag == "blue_flag" then
		flagTbl = GAMEMODE.FlagSpawn[game.GetMap()].Blue
	else
		flagTbl = GAMEMODE.FlagSpawn[game.GetMap()].Red
	end
	if pos then
		-- hacky method for making the flag not be in the ground if spawning from player holstering the flag
		pos.z = pos.z + 45
	else
		-- use the position from the flag table if pos is not defined
		pos = flagTbl.Pos
	end
	ang = ang and ang or flagTbl.Ang
	local flagEnt = ents.Create(flag)
	flagEnt:Spawn()
	flagEnt:SetPos(pos)
	flagEnt:SetAngles(ang)
	flagEnt.NoPickup = true
	
	timer.Simple(2,function() flagEnt.NoPickup = false end)
	
	if ret then
		timer.Simple(15,function()
			if flagEnt:IsValid() then
				flagEnt:SetPos(flagTbl.Pos)
				flagEnt:SetAngles(flagTbl.Ang)
				if flag == "blue_flag" then
					-- net message for return announcement
				else
					-- net message for return announcement
				end
			end
		end)
	end
	
	return flagEnt
end

end
[/lua]

One issue I seem to be running into is whenever the flag is dropped (SWEP holstered) it either drops the flag as it is supposed to, or it crashes the server with no error. When running it on a local game it just shuts down the game and no engine error or anything, just seems as if the game is closed normally.

I have a secondary function that runs to spawn all flags (GM:SpawnFlags) which is used at the end of the 10 captures to reset the map and reset teams, plus the SpawnFlag function is used to reset a single flag when the flag is captured (using just the flag variable, the rest are nil) and it works perfectly fine and causes no crashes. So the issue seems to only persist when SWEP:Holster() is called.

I could potentially remove the flag removal check (the for loop in the SpawnFlag) as it was only there because my old method was to use ENT:Think on the flag to check if it was attached (via a network bool and int) to a player and have it set its position to the bone position of the player. Now whenever the flag is picked up the entity is removed (self:Remove() in the ENT code on touch and use) and the proper SWEP is given based on the flag being picked up. Only really need it to run at the end of the capture round when the flags are both resetting and teams swapping.

So, all that being said, any ideas on what could be causing the issue?

Here’s a small clip of it happening while I was trying to record a video to showcase the progress: http://www.twitch.tv/karmalord/c/3122598 (was on a dedicated server hence it not crashing to desktop, just a connection problem)

Maybe you’re not supposed to delete a weapon while it is being holstered. Try commenting out the StripWeapon line, see if it still crashes. If it doesn’t crash anymore, the problem comes from there. Using timer.Simple with a delay of 0 to remove the weapon right after it is holstered would probably work then.

I’ll try it but I doubt it since it works half the time.

Edit:

Nope. Didn’t work. Crashed after switching on the third time.

Edit 2:

Apparently this made it work:

[lua]
function SWEP:Holster()
if SERVER then
local pos = self.Owner:GetPos()
self.Owner:StripWeapon(self:GetClass())
timer.Simple(0.2,function() GAMEMODE:SpawnFlag(“blue_flag”,pos,nil,true) end)
end
end
[/lua]