Timer error with bomb melon.

Well my melon spawns, has a trail, removes, and explodes.

But only the last one that i shot explodes, all the others keep looping their timers and not doing anything.

And it is also invisible :open_mouth:

Here are the errors:

Timer Error: entities/bomb_melon/shared.lua:20: Tried to use a NULL entity!

entities/bomb_melon/shared.lua:33: attempt to call field ‘SpriteTrail’ (a nil value)
the melon does have a trail…

Upload them to a site like imgkk and link them with img tags.

[editline]06:26PM[/editline]

Also I suspect what you’re doing is naming the timers the same thing, which overwrites any other timers with the same name.

oh so how do I change the timer name with each entity spawned?

Make a variable that is incremented each time one is spawned, then name it something like “melontimer” … varname

The … is the concatenation operator, if you didn’t know.

Just post some of the code.

I think he should at least try to fix it himself first, if what I posted actually is the problem.

[editline]06:49PM[/editline]

Which it probably is.

Here’s the code:



ENT.Type 			= "anim"
ENT.Base 			= "base_entity"

ENT.PrintName		= "bomb_melon"
ENT.Author		= "Sonic Cow"
ENT.Contact		= "n/a"

ENT.Spawnable		= false
ENT.AdminSpawnable	= false

AddCSLuaFile( "shared.lua" )

/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize() 

	timer.Create( "Timer", 2.5, 0, function()
		local explode = ents.Create( "env_explosion" )
		explode:SetPos( self:GetPos() )
		explode:SetOwner( self.Owner )
		explode:Spawn()
		explode:SetKeyValue( "iMagnitude", "75" )
		explode:Fire( "Explode", 0, 0 )
		explode:EmitSound( "k_lab2.Barney_Explosion", 400, 400 )
		self:Remove()
	end )

	self.Entity:SetModel( "models/props_junk/watermelon01.mdl" )
	self.Entity:PhysicsInit( SOLD_BBOX )
	self.Entity:SetMoveType( MOVETYPE_FLYGRAVITY )
	self.Entity:SetSolid( SOLD_BBOX )
	local trail = util.SpriteTrail(self, 0, Color(0,255,0), false, 5, 1, 4, 1/(15+1)*0.5, "trails/plasma.vmt")
end


EDIT:

And I did try to solve it.
I spent 3 hour just staring at that.

Yeah for every melon you make, it overrides the previous timer. Gotta increment a variable and put it in the name, or something that will make the name change each time.

[editline]06:56PM[/editline]

[lua]
ENT.Type = “anim”
ENT.Base = “base_entity”

ENT.PrintName = “bomb_melon”
ENT.Author = “Sonic Cow”
ENT.Contact = “n/a”

ENT.Spawnable = false
ENT.AdminSpawnable = false
ENT.timernum = 0

AddCSLuaFile( “shared.lua” )

/---------------------------------------------------------
Name: Initialize
---------------------------------------------------------
/
function ENT:Initialize()

timer.Create( "Timer" .. ENT.timernum, 2.5, 0, function()
	local explode = ents.Create( "env_explosion" )
	explode:SetPos( self:GetPos() )
	explode:SetOwner( self.Owner )
	explode:Spawn()
	explode:SetKeyValue( "iMagnitude", "75" )
	explode:Fire( "Explode", 0, 0 )
	explode:EmitSound( "k_lab2.Barney_Explosion", 400, 400 )
	self:Remove()
end )

self.Entity:SetModel( "models/props_junk/watermelon01.mdl" )
self.Entity:PhysicsInit( SOLD_BBOX )
self.Entity:SetMoveType( MOVETYPE_FLYGRAVITY )
self.Entity:SetSolid( SOLD_BBOX )
local trail = util.SpriteTrail(self, 0, Color(0,255,0), false, 5, 1, 4, 1/(15+1)*0.5, "trails/plasma.vmt")
   ENT.timernum = ENT.timernum + 1

end
[/lua]

Try that. Never made an ent before so idk if it will work.
Also, lua tags.

So how do I… do that…

I know it’s global variables.

If you didn’t see that post above, there it is lol.

If I get this wrong, Then sorry, because I don’t do much work with entities, but try this:
[lua]
ENT.Type = “anim”
ENT.Base = “base_entity”

ENT.PrintName = “bomb_melon”
ENT.Author = “Sonic Cow”
ENT.Contact = “n/a”

ENT.Spawnable = false
ENT.AdminSpawnable = false

AddCSLuaFile( “shared.lua” )

/---------------------------------------------------------
Name: Initialize
---------------------------------------------------------
/
function ENT:Initialize()

timer.Create( "Timer", 2.5, 0, function()
	local explode = ents.Create( "env_explosion" )
	explode:SetOwner( self.Owner )
	explode:SetPos( self.Owner:GetPos() )
	explode:Spawn()
	explode:SetKeyValue( "iMagnitude", "75" )
	explode:Fire( "Explode", 0, 0 )
	explode:EmitSound( "k_lab2.Barney_Explosion", 400, 400 )
	self:Remove()
end )

self.Entity:SetModel( "models/props_junk/watermelon01.mdl" )
self.Entity:PhysicsInit( SOLD_BBOX )
self.Entity:SetMoveType( MOVETYPE_FLYGRAVITY )
self.Entity:SetSolid( SOLD_BBOX )

if CLIENT then
local trail = util.SpriteTrail(self, 0, Color(0,255,0), false, 5, 1, 4, 1/(15+1)*0.5, “trails/plasma.vmt”)
end
end[/lua]

@ yakahughes

ur script didn’t work

and this is the error:

entities/bomb_melon/shared.lua:19: attempt to index global ‘ENT’ (a nil value)

That’ll still override each previous melon’s timer I think though Jamie.

[editline]07:07PM[/editline]

Oh, try this:

[lua]
ENT.Type = “anim”
ENT.Base = “base_entity”

ENT.PrintName = “bomb_melon”
ENT.Author = “Sonic Cow”
ENT.Contact = “n/a”

ENT.Spawnable = false
ENT.AdminSpawnable = false
ENT.timernum = 0

AddCSLuaFile( “shared.lua” )

/---------------------------------------------------------
Name: Initialize
---------------------------------------------------------
/
function ENT:Initialize()

timer.Create( "Timer" .. self.timernum, 2.5, 0, function()
	local explode = ents.Create( "env_explosion" )
	explode:SetPos( self:GetPos() )
	explode:SetOwner( self.Owner )
	explode:Spawn()
	explode:SetKeyValue( "iMagnitude", "75" )
	explode:Fire( "Explode", 0, 0 )
	explode:EmitSound( "k_lab2.Barney_Explosion", 400, 400 )
	self:Remove()
end )

self.Entity:SetModel( "models/props_junk/watermelon01.mdl" )
self.Entity:PhysicsInit( SOLD_BBOX )
self.Entity:SetMoveType( MOVETYPE_FLYGRAVITY )
self.Entity:SetSolid( SOLD_BBOX )
    self.timernum = self.timernum + 1
    if CLIENT then
      local trail = util.SpriteTrail(self, 0, Color(0,255,0), false, 5, 1, 4, 1/(15+1)*0.5, "trails/plasma.vmt")
         
    end

end
[/lua]

EDIT: Updated to include jamie’s thing.
EDIT: Updated to fix a stupid error.

K ima go test

EDIT:

Nope :frowning:

Not working and also the trails are now messed up.

And the timer keeps looping even after the entity got removed.

EDIT:

So would I add:

timer.Stop(“Timer”)

?

EDIT:

The trails are on

Oh yeah, you would need to remove the timer, duh.

If you only want the timer to run once, change the 0 in

timer.Create( “Timer” … self.timernum, 2.5, 0, function()

to a 1.

timer.Simple, god fucking damnit.

[lua]ENT.Type = “anim”
ENT.Base = “base_entity”

ENT.PrintName = “bomb_melon”
ENT.Author = “Sonic Cow”
ENT.Contact = “n/a”

ENT.Spawnable = false
ENT.AdminSpawnable = false
ENT.timernum = 0

AddCSLuaFile( “shared.lua” )

/---------------------------------------------------------
Name: Initialize
---------------------------------------------------------
/
function ENT:Initialize()

timer.Simple(2.5, function(ent)
	if not ValidEntity(ent) then return end
	local explode = ents.Create( "env_explosion" )
	explode:SetPos( ent:GetPos() )
	explode:SetOwner( ent.Owner )
	explode:Spawn()
	explode:SetKeyValue( "iMagnitude", "75" )
	explode:Fire( "Explode", 0, 0 )
	explode:EmitSound( "k_lab2.Barney_Explosion", 400, 400 )
	ent:Remove()
end, self)

self.Entity:SetModel( "models/props_junk/watermelon01.mdl" )
self.Entity:PhysicsInit( SOLD_BBOX )
self.Entity:SetMoveType( MOVETYPE_FLYGRAVITY )
self.Entity:SetSolid( SOLD_BBOX )
    self.timernum = self.timernum + 1
    if CLIENT then
      local trail = util.SpriteTrail(self, 0, Color(0,255,0), false, 5, 1, 4, 1/(15+1)*0.5, "trails/plasma.vmt")
         
    end

end[/lua]

Also, it’s safer if you send “self” to the function through a function argument (here, I called it “ent”), so you are 100% sure that function knows which entity you are talking about.
And check if the entity still exists in the timer, because if it happens to be removed by something else before the timer expires, you’ll get a timer error.

[editline]08:43AM[/editline]

[lua]self.Entity:PhysicsInit( SOLD_BBOX )

self.Entity:SetSolid( SOLD_BBOX )[/lua]

You should consider fixing this, it’s SOLID_BBOX.

Guys i fixed it. :slight_smile:

But i still have the invisible melon problem :[