timer.simple delete created entity if time is >180

So Iv been trying for the last 2 days to get this working but the problem is that there are no errors in the console to tell me what is wrong, it just does not work…
Im trying to make this:


 function timer.Simple( 180, function ents.FindByClass( tDropItems )[1]:Remove() )

But it does not seem to be working, even so the code seems like it should work, right?
Here is the full script for some context.



local tDropItems = {
	"doi_atow_ammotypes_rifles",
	"doi_atow_ammotypes_rifles",
	"doi_atow_ammotypes_rifles",
	"doi_atow_ammotypes_shotguns",
	"doi_atow_attpack_boltactions",
	"doi_atow_attpack_c96",
	"doi_atow_attpack_luger",
	"doi_atow_attpack_c96car",
	"doi_atow_attpack_mp34",
	"doi_atow_attpack_descopes",
	"doi_atow_attpack_bhp",
	"doi_atow_attpack_ithacam37",
	"doi_atow_attpack_gbscopes",
	"doi_atow_attpack_stenmk2",
	"doi_atow_attpack_lmgs",
	"doi_atow_attpack_finishes",
	"doi_atow_attpack_misc",
	"doi_atow_attpack_m1carbine",
	"doi_atow_attpack_m1911kit",
	"doi_atow_attpack_m3",
	"doi_atow_attpack_usscopes",
	"doi_atow_attpack_thompson",
	"cw_ammo_kit_small",
	"cw_ammo_kit_small",
	"cw_ammo_kit_small",
	"item_healthkit",
	"doi_atow_cwbinoculars",
	"doi_atow_usbinoculars",
}

local iItemCount = #tDropItems

function GM:PlayerDeath(pPlayer)
	local pEntity = ents.Create(tDropItems[math.random(1, iItemCount)])
	
	if (pEntity:IsValid()) then
		pEntity:SetPos(pPlayer:GetPos())
		pEntity:SetAngles(AngleRand())
		pEntity:Spawn()
	end
end


Here is how the new string would go in



function GM:PlayerDeath(pPlayer)
	local pEntity = ents.Create(tDropItems[math.random(1, iItemCount)])
	
	if (pEntity:IsValid()) then
		pEntity:SetPos(pPlayer:GetPos())
		pEntity:SetAngles(AngleRand())
		pEntity:Spawn()
                    function timer.Simple( 180, function ents.FindByClass( tDropItems )[1]:Remove() )
                    end
	end
end


[lua]function GM:PlayerDeath(pPlayer)
local pEntity = ents.Create(tDropItems[math.random(1, iItemCount)])

if IsValid(pEntity) then
	pEntity:SetPos(pPlayer:GetPos())
	pEntity:SetAngles(AngleRand())
	pEntity:Spawn()
    timer.Simple( 180, function 
		if IsValid(pEntity) then 
			pEntity:Remove()
		end
	end)
end

end[/lua]

Try this

That’s not how you layout timer functions.

  • Don’t put function before timer (Unless you’re trying to overwrite it for some reason)
  • the second argument in timer.Simple is a function, which means it should probably start with function()
  • all functions have to be ended, so add an end before the last parenthesis

timer.Simple( 180, function() ents.FindByClass( tDropItems )[1]:Remove() end)

One more thing - you are not validating whether the entity you are trying to remove actually exists.
Also keep in mind that timer.Simple cannot be stopped, so you may launch more than 1 timer with same action.

didn’t work sadly, I did get an error msg saying “expected ‘(’ near if” I tried adding it and then keep on adding stuff that the error msg said it needed, it ended with another error that was not logged.

[editline]14th August 2017[/editline]

I implemented the code like this:


function GM:PlayerDeath(pPlayer)
	local pEntity = ents.Create(tDropItems[math.random(1, iItemCount)])
	
	if (pEntity:IsValid()) then
		pEntity:SetPos(pPlayer:GetPos())
		pEntity:SetAngles(AngleRand())
		pEntity:Spawn()
		timer.Simple( 15, function() ents.FindByClass( tDropItems )[1]:Remove() end)
	end
end

This is the error I got:


[ERROR] gamemodes/dls/gamemode/init.lua:134: bad argument #1 to 'FindByClass' (string expected, got table)
  1. FindByClass - [C]:-1
   2. unknown - gamemodes/dls/gamemode/init.lua:134

Timer Failed! [Simple][@gamemodes/dls/gamemode/init.lua (line 134)]


I dont understand why it failed if


tDropItems

is supposed to be the class…

the idea of


timer.Simple( 15, function() ents.FindByClass( Entity:GetClass )[1]:Remove() end)

passed my mind, but it failed too…

My Bad

[lua]function GM:PlayerDeath(pPlayer)
local pEntity = ents.Create(tDropItems[math.random(1, iItemCount)])

if IsValid(pEntity) then
	pEntity:SetPos(pPlayer:GetPos())
	pEntity:SetAngles(AngleRand())
	pEntity:Spawn()
    timer.Simple( 180, function() 
		if IsValid(pEntity) then 
			pEntity:Remove()
		end
	end)
end

end[/lua]

Nice! So what happened is that Entity had to be renamed to pEntity because that is the name of the entity right?