Tried to use a NULL entity?!

Hello, I’ve been having this issue ALL day. This is returning an error saying tried to use a NULL entity! I don’t know why.

Here’s the error in console:



[ERROR] gamemodes/terrortown/gamemode/ent_replace.lua:125: Tried to use a NULL entity!
  1. SetPos - [C]:-1
   2. ReplaceWeapons - gamemodes/terrortown/gamemode/ent_replace.lua:125
    3. ReplaceEntities - gamemodes/terrortown/gamemode/ent_replace.lua:159
     4. unknown - gamemodes/terrortown/gamemode/init.lua:388


[ERROR] gamemodes/terrortown/gamemode/ent_replace.lua:125: Tried to use a NULL entity!
  1. SetPos - [C]:-1
   2. ReplaceWeapons - gamemodes/terrortown/gamemode/ent_replace.lua:125
    3. ReplaceEntities - gamemodes/terrortown/gamemode/ent_replace.lua:159
     4. unknown - gamemodes/terrortown/gamemode/init.lua:388


Line 125 is


newweps:Activate()

.

Here’s my code:


local function ReplaceWeapons()
	for _, ent in pairs(table.Merge(ents.FindByClass("weapon_ttt*"), ents.FindByClass("weapon_zm*"))) do
		if ValidEntity(ent) then
		local weaponpos = ent:GetPos()
		randomnewweps = {}
		randomnewweps[1]="weapon_cstm_pistol_deagle"
		randomnewweps[2]="weapon_cstm_pistol_fiveseven"
		randomnewweps[3]="weapon_cstm_pistol_glock"
		randomnewweps[4]="weapon_cstm_pistol_p228"
		randomnewweps[5]="weapon_cstm_rif_ak47"
		randomnewweps[6]="weapon_cstm_rif_famas"
		randomnewweps[7]="weapon_cstm_rif_m249"
		randomnewweps[8]="weapon_cstm_shotgun_shotgun"
		randomnewweps[9]="weapon_cstm_smg_mac10"
		randomnewweps[10]="weapon_cstm_smg_p90"
		randomnewweps[11]="weapon_cstm_smg_tmp"
		randomnewweps[12]="weapon_cstm_smg_ump45"
		randomnewweps[13]="weapon_cstm_snip_rifle"
		randomnewweps[14]="weapon_cstm_sniper_aug"
		randomnewweps[15]="weapon_cstm_sniper_sg550"
		randomnewweps[16]="weapon_cstm_sniper_sg552"
		if ValidEntity(ent) then
		local newweps = ents.Create(table.Random(randomnewweps))
		newweps:SetPos(weaponpos)
		newweps:Spawn()
		newweps:Activate()
		print(tostring(newweps).. " successfully replaced " ..tostring(ent))
		ent:Remove()
		end
		end
	end
end

print newweps I imagine it is not able to create any of the weapon entities since they don’t exist

The weapon entites do exist, but i don’t know why it’s giving this error.

print newweps and confirm it then

This is what printed:


Weapon [1233][weapon_cstm_pistol_deagle] successfully replaced Weapon [969][weapon_zm_shotgun]
Weapon [1233][weapon_cstm_pistol_deagle]
Weapon [1234][weapon_cstm_smg_tmp] successfully replaced Weapon [974][weapon_zm_shotgun]
Weapon [1234][weapon_cstm_smg_tmp]
Weapon [1235][weapon_cstm_smg_p90] successfully replaced Weapon [975][weapon_zm_shotgun]
Weapon [1235][weapon_cstm_smg_p90]
Weapon [1236][weapon_cstm_sniper_sg550] successfully replaced Weapon [987][weapon_zm_pistol]
Weapon [1236][weapon_cstm_sniper_sg550]
Weapon [1237][weapon_cstm_smg_mac10] successfully replaced Weapon [988][weapon_zm_pistol]
Weapon [1237][weapon_cstm_smg_mac10]
Weapon [1238][weapon_cstm_smg_tmp] successfully replaced Weapon [992][weapon_zm_sledge]
Weapon [1238][weapon_cstm_smg_tmp]
Weapon [1239][weapon_cstm_snip_rifle] successfully replaced Weapon [993][weapon_zm_sledge]
Weapon [1239][weapon_cstm_snip_rifle]
Weapon [1240][weapon_cstm_pistol_fiveseven] successfully replaced Weapon [994][weapon_zm_sledge]
Weapon [1240][weapon_cstm_pistol_fiveseven]
Weapon [1241][weapon_cstm_rif_famas] successfully replaced Weapon [1030][weapon_zm_molotov]
Weapon [1241][weapon_cstm_rif_famas]
Weapon [1242][weapon_cstm_smg_p90] successfully replaced Weapon [1031][weapon_zm_molotov]
Weapon [1242][weapon_cstm_smg_p90]
Weapon [1243][weapon_cstm_smg_p90] successfully replaced Weapon [1032][weapon_zm_molotov]
Weapon [1243][weapon_cstm_smg_p90]
Weapon [1244][weapon_cstm_shotgun_shotgun] successfully replaced Weapon [1033][weapon_zm_molotov]
Weapon [1244][weapon_cstm_shotgun_shotgun]
Weapon [1245][weapon_cstm_rif_famas] successfully replaced Weapon [1034][weapon_zm_molotov]
Weapon [1245][weapon_cstm_rif_famas]
Weapon [1246][weapon_cstm_rif_m249] successfully replaced Weapon [1035][weapon_zm_molotov]
Weapon [1246][weapon_cstm_rif_m249]
Weapon [1247][weapon_cstm_sniper_sg552] successfully replaced Weapon [1036][weapon_zm_molotov]
Weapon [1247][weapon_cstm_sniper_sg552]
Weapon [1248][weapon_cstm_pistol_fiveseven] successfully replaced Weapon [1037][weapon_zm_molotov]
Weapon [1248][weapon_cstm_pistol_fiveseven]
Weapon [1249][weapon_cstm_pistol_deagle] successfully replaced Weapon [1038][weapon_zm_pistol]
Weapon [1249][weapon_cstm_pistol_deagle]
Weapon [1250][weapon_cstm_pistol_p228] successfully replaced Weapon [1039][weapon_zm_pistol]
Weapon [1250][weapon_cstm_pistol_p228]
Weapon [1251][weapon_cstm_rif_ak47] successfully replaced Weapon [1040][weapon_zm_pistol]
Weapon [1251][weapon_cstm_rif_ak47]
Weapon [1252][weapon_cstm_pistol_p228] successfully replaced Weapon [1081][weapon_zm_molotov]
Weapon [1252][weapon_cstm_pistol_p228]
Weapon [1253][weapon_cstm_rif_ak47] successfully replaced Weapon [1082][weapon_zm_sledge]
Weapon [1253][weapon_cstm_rif_ak47]
Weapon [1254][weapon_cstm_smg_mac10] successfully replaced Weapon [1094][weapon_zm_revolver]
Weapon [1254][weapon_cstm_smg_mac10]
Weapon [1255][weapon_cstm_sniper_sg550] successfully replaced Weapon [1095][weapon_zm_pistol]
Weapon [1255][weapon_cstm_sniper_sg550]
Weapon [1256][weapon_cstm_smg_tmp] successfully replaced Weapon [1102][weapon_zm_revolver]
Weapon [1256][weapon_cstm_smg_tmp]
Weapon [1257][weapon_cstm_rif_ak47] successfully replaced Weapon [1106][weapon_zm_shotgun]
Weapon [1257][weapon_cstm_rif_ak47]
Weapon [1258][weapon_cstm_rif_m249] successfully replaced Weapon [1119][weapon_zm_revolver]
Weapon [1258][weapon_cstm_rif_m249]
Weapon [1259][weapon_cstm_pistol_fiveseven] successfully replaced Weapon [1123][weapon_zm_molotov]
Weapon [1259][weapon_cstm_pistol_fiveseven]
Weapon [1260][weapon_cstm_smg_tmp] successfully replaced Weapon [346][weapon_zm_bat]
Weapon [1260][weapon_cstm_smg_tmp]
Weapon [1261][weapon_cstm_rif_ak47] successfully replaced Weapon [347][weapon_zm_carry]
Weapon [1261][weapon_cstm_rif_ak47]


They do in fact DO print. There’s more but too much to copy.

[editline]30th August 2013[/editline]

For some reason, It just magically started working. I don’t know how/why.