timer.simple delete created entity if time is >180
6 replies, posted
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:
[code] function timer.Simple( 180, function ents.FindByClass( tDropItems )[1]:Remove() )[/code]
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.
[code]
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
[/code]
Here is how the new string would go in
[code]
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
[/code]
[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
[QUOTE=alemismun;52571436]
[code] function timer.Simple( 180, function ents.FindByClass( tDropItems )[1]:Remove() )[/code]
[/QUOTE]
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
[code]timer.Simple( 180, function() ents.FindByClass( tDropItems )[1]:Remove() end)[/code]
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.
[QUOTE=Zeh Matt;52571470][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[/QUOTE]
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]
[QUOTE=kpjVideo;52571567]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
[code]timer.Simple( 180, function() ents.FindByClass( tDropItems )[1]:Remove() end)[/code][/QUOTE]
I implemented the code like this:
[code]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[/code]
This is the error I got:
[code][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)]
[/code]
I dont understand why it failed if [code]tDropItems[/code] is supposed to be the class...
the idea of [code]timer.Simple( 15, function() ents.FindByClass( Entity:GetClass )[1]:Remove() end)[/code] passed my mind, but it failed too...
[QUOTE=alemismun;52571706]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:
[code]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[/code]
This is the error I got:
[code][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)]
[/code]
I dont understand why it failed if [code]tDropItems[/code] is supposed to be the class...
the idea of [code]timer.Simple( 15, function() ents.FindByClass( Entity:GetClass )[1]:Remove() end)[/code] passed my mind, but it failed too...[/QUOTE]
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]
[QUOTE=Zeh Matt;52572113]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][/QUOTE]
Nice! So what happened is that Entity had to be renamed to pEntity because that is the name of the entity right?
Sorry, you need to Log In to post a reply to this thread.