Spawning a entity from an entity

So im working on a Drug Refinery and i have it set with 7 stages each stage it the model changes to a darker color green and when it reaches stage seven it will be fully green. When it reaches stage 7 you can sell the drugs for money by pressing E on it. But i want to make it more complex.


	-- Timers
	timer.Simple(tonumber(PLANT_CONFIG.Stage1) or 300, function()
		if !IsValid(self) then return end
        self:SetColor( Color( 0, 35, 0, 255) )
		PrintMessage( HUD_PRINTTALK, "Your drugs reached stage 1!")
		
		timer.Simple(tonumber(PLANT_CONFIG.Stage2) or 30, function()
			if !IsValid(self) then return end
            self:SetColor( Color( 0, 60, 0, 255) )
			PrintMessage( HUD_PRINTTALK, "Your drugs reached stage 2!")
			
			timer.Simple(tonumber(PLANT_CONFIG.Stage3) or 30, function()
				if !IsValid(self) then return end
                self:SetColor( Color( 0, 100, 0, 255) )
				PrintMessage( HUD_PRINTTALK, "Your drugs reached stage 3!")
				
				timer.Simple(tonumber(PLANT_CONFIG.Stage4) or 30, function()
					if !IsValid(self) then return end
                    self:SetColor( Color( 0, 135, 0, 255) )
					PrintMessage( HUD_PRINTTALK, "Your drugs reached stage 4!")
					
					timer.Simple(tonumber(PLANT_CONFIG.Stage5) or 30, function()
						if !IsValid(self) then return end
                        self:SetColor( Color( 0, 170, 0, 255) )
						PrintMessage( HUD_PRINTTALK, "Your drugs reached stage 5!")
						
						timer.Simple(tonumber(PLANT_CONFIG.Stage6) or 30, function()
							if !IsValid(self) then return end
                            self:SetColor( Color( 0, 200, 0, 255) )
							PrintMessage( HUD_PRINTTALK, "Your drugs reached stage 6!")
							
							timer.Simple(tonumber(PLANT_CONFIG.Stage7) or 30, function()
								if !IsValid(self) then return end
								self:SetColor( Color( 0, 255, 0, 255) )
								PrintMessage( HUD_PRINTTALK, "Your drugs in the refinery reached stage 7 and can now be sold for money!")
								self.isUsable = true
								self.magic = true -- Final stage will display a small effect when it's done.
							end)
						end)
					end)
				end)
			end)
		end)
	end)
end

That is the code that defindes what happens after each stage as you can see it tells them that there refinery reaches the next stage but i want to know how i can make my own entity spawn for example i made a cocaine entity i wanted to make that spawn every other stage so at stage 2 cocaine would spawn and i could pick it up level 4 another would spawn. Really all i need to know is what code i need to spawn the entity through code.


timer.Simple(tonumber(PLANT_CONFIG.Stage2) or 30, function()
			if !IsValid(self) then return end
            self:SetColor( Color( 0, 60, 0, 255) )
			PrintMessage( HUD_PRINTTALK, "Your drugs reached stage 2!")

After the print message.

[LUA] local entCocaine = ents.Create(<the cocaine ent’s name>)
entCocaine:SetPos(self:GetPos()+self:GetForward()*20) //The cocaine will spawn 20 world units forward of “self”, it may get stuck in walls or something, but it saves it from getting stuck inside the spawner.
entCocaine:Spawn()[/LUA]
I suppose that’s what you’re looking for?
Then put it in to a function, like so:
[LUA] function ENT:SpawnCocaine()
local entCocaine = ents.Create(<the cocaine ent’s name>)
entCocaine:SetPos(self:GetPos()+self:GetForward()*20) //The cocaine will spawn 20 world units forward of “self”, it may get stuck in walls or something, but it saves it from getting stuck inside the spawner.
entCocaine:Spawn()
end[/LUA]
I suppose you could use:
[LUA] entCocaine:SetCollisionGroup(COLLISION_GROUP_DEBRIS) [/LUA]
That’ll make it nocollide with other entities, as well as the spawner, but you’d have to move the spawning entity in order to take the cocaine, and if you DO use this, remove the (+self:GetForward()*20)
Then call the function on each timer?

I seem to get a error it is interrupting the timer causing this error


[ERROR] gamemodes/darkrp/entities/entities/drug_refinery/init.lua:75: attempt to index global 'ENT' (a nil value)
  1. unknown - gamemodes/darkrp/entities/entities/drug_refinery/init.lua:75

Timer Failed! [Simple][@gamemodes/darkrp/entities/entities/drug_refinery/init.lua (line 70)]


This is what i put in



-- Timers
	timer.Simple(tonumber(PLANT_CONFIG.Stage1) or 300, function()
		if !IsValid(self) then return end
        self:SetColor( Color( 0, 35, 0, 255) )
		PrintMessage( HUD_PRINTTALK, "Your drugs reached stage 1!")	

function ENT:SpawnCocaine()
    local entCocaine = ents.Create(seed_weed)
         entCocaine:SetPos(self:GetPos()+self:GetForward()*20) //The cocaine will spawn 20 world units forward of "self", it may get stuck in walls or something, but it saves it from getting stuck inside the spawner.
         entCocaine:Spawn()
    end

Then the next timer started here

Replace “ENT” in the function title with whatever the spawner entity is referred to as.
(May be self:SpawnCocaine())

Ok that got rid of the error but the entity isn’t spawning i’m using an entity called seed_weed that i places in


function self:SpawnCocaine()
    local entCocaine = ents.Create("seed_weed")
        entCocaine:SetPos(self:GetPos()+self:GetForward()*20) //The cocaine will spawn 20 world units forward of "self", it may get stuck in walls or something, but it saves it from getting stuck inside the spawner.
        entCocaine:Spawn()
end

The Drug Refinery still goes through the other steps just the entity isn’t spawning.

Okay, maybe… lose the (+self:GetForward()*20) and add entCocaine:SetCollisionGroup(COLLISION_GROUP_DEBRIS) as I mentioned before… should spawn within the spawner with no collision.

So it should look like this?


function self:SpawnCocaine()
    local entCocaine = ents.Create("seed_weed")
        entCocaine:SetPos(self:GetPos()
		entCocaine:SetCollisionGroup(COLLISION_GROUP_DEBRIS) 
        entCocaine:Spawn()
end

This is my first time doing this obviously so im having a hard time with this one.

Seems alright to me, also… it seems it’s taking a while between replies, I can only guess you’re restarting gmod to reload the code, if that’s the case, editing the code should automatically update it… no restart required.

Oh no i was just toying around with the code a bit and just now i was afk for a min.

And with that code the entity still isn’t spawning no error but it’s not spawning the seed_weed entity.

I think i may just have to drop this idea. I have tried multiple codes for spawning an entity and all 3 haven’t given errors but just won’t spawn the entity.

How peculiar… I have no idea why that’s not working, I’m only an SNPC maker, but I’ve made entities before… they’re called normally… I don’t think I can help from this point on…
Well, instead of calling the function using a name and such… you could place the contents of the function in to each timer… but that’d take up alot of space and probably isn’t worth it.

Yea man well can’t say we didn’t try lol. Thanks a lot for your help!