Entities Respawning Help

No errors in console, due to there being no error, just a lack of Lua Knowledge.

Goal i’m trying to achieve:
Entities (randomly selected) spawn on map in exact locations [Done]
Entities (randomly selected) RE-spawning on map after 7 minutes in exact same locations [Need Help]

Init.lua

-The table of entities i make the game choose at random from.


ItemTable = {}
ItemTable[1] = "watermelon"
ItemTable[2] = "milk"

The code that spawns the entities on the maps when it’s a new map changes/fresh restart.


function GM:InitPostEntity( )

	local ent = ents.Create(table.Random( ItemTable ))
		ent:Spawn()
		ent:SetPos( Vector( -169.696579, 1135.181396, 135.0000 ) ); -- in broken building
		
	local ent2 = ents.Create(table.Random( ItemTable ))
		ent2:Spawn()
		ent2:SetPos( Vector( -2000.762817, 1181.251221, 168.000000 ) ); -- hidden room

	local ent3 = ents.Create(table.Random( ItemTable ))
		ent3:Spawn()
		ent3:SetPos( Vector(-1551.812134, 1083.409668, 263.282471) ); -- above hidden room
		
	local ent4 = ents.Create(table.Random( ItemTable ))
		ent4:Spawn()
		ent4:SetPos( Vector(-986.553955, 1528.284424, 168.727219) ); -- middle on table
		
	local ent5 = ents.Create(table.Random( ItemTable ))
		ent5:Spawn()
		ent5:SetPos( Vector(-36.200439, 1059.177246, 265.023651) ); -- above 'orange door'
end

watermelon.lua
-A lot of the stuff in here is for debug purposes only, the actual functionality of the entity will be changed when i figure out how to make them respawn. Same goes for milk.lua


AddCSLuaFile()

ENT.Type	=	"anim" -- allows movement/interaction
ENT.Base	=	"base_gmodentity" -- give a base

ENT.PrintName	=	"Watermelon" 
ENT.Author		=	"Bitter"
ENT.Purpose		=	"Press 'USE' to pick up."
ENT.Instructions	=	"Press 'USE' to pick up."

ENT.Spawnable	=	false
ENT.AdminSpawnable	=	false


function ENT:Initialize()

	self:SetHealth( 100 )
	self:SetModel("models/props_junk/watermelon01.mdl")
	self:SetSolid( 2 ) -- or "SOLID_NONE"
	self:SetMoveType( MOVETYPE_NONE )
	
	local phs = self:GetPhysicsObject()
	if (phs:IsValid() ) then
	
		phs:Wake() -- wakes it up and makes it follow the physics i have set.
	
	end

end

function ENT:Draw()
	self:DrawModel() -- draw it for players to see :p
end

function ENT:Use( ply ) -- thing that touches the ent. player.

	if (ply:IsPlayer() ) then
	local wheretorespawn = self:GetPos()
		ply:Kill()
		self:Remove() -- remove the ent
			timer.Create( "respawntimer", 3, 0, function()
			self:Respawn()
			end)
	end


end



milk.lua
-Pretty much the same as watermelon.lua. I just want to make sure all the info is out there for interpretation and to avoid ‘how can we help if you don’t give us enough info’ xD.


AddCSLuaFile()

ENT.Type	=	"anim" -- allows movement/interaction
ENT.Base	=	"base_gmodentity" -- give a base

ENT.PrintName	=	"Milk" 
ENT.Author		=	"Bitter"
ENT.Purpose		=	"Press 'USE' to pick up."
ENT.Instructions	=	"Press 'USE' to pick up."

ENT.Spawnable	=	false
ENT.AdminSpawnable	=	false


function ENT:Initialize()

	self:SetHealth( 100 )
	self:SetModel("models/props_junk/garbage_milkcarton001a.mdl")
	self:SetSolid( 2 ) -- or "SOLID_NONE"
	self:SetMoveType( 0 )
	
	--local phs = self:GetPhysicsObject()
	--if (phs:IsValid() ) then
	
	--	phs:Wake() -- wakes it up and makes it follow the physics i have set.
	
	--end

end


function ENT:Draw()
	self:DrawModel() -- draw it for players to see :p
end



function ENT:Use( ply ) -- thing that touches the ent. player.

	if (ply:IsPlayer() ) then
		ply:Kill()
		self:Remove() -- remove the ent
			timer.Create( "respawntimer", 3, 0, function()
			self:Respawn()
			end)
	end


end






As you can see, i’ve tried to set a timer to respawn them, but nothing happens. No errors in console either.

To recap: I want these two entities to be respawning on the map x seconds after ENT:Use (aka it’s eaten) at random.

Something i want to avoid: As it stands, the entities respawn at a static respawn point. The entities (either milk or watermelon) will spawn at random.
I DO NOT WANT the entity to respawn as the entity that was there before. I want it to to be random.

Explanation of the above:
>map starts
>watermelon spawns at position 2
>I eat or ‘use’ watermelon at position 2
>x seconds later is respawns as watermelon OR milk. Not just watermelon.
>rinse and repeat.

Sorry if i am/was over-specific. I just really want to give everyone a good idea of what it is that i’m trying to accomplish.

From that page:

So what you can do is when the entity is removed, create a timer, and then run something like this within it…
[lua]
local ent = ents.Create( “milk” )
ent:SetPos( desiredPosition )
ent:Spawn( )
[/lua]

Where would i do this?
In the insertentitynamehere.lua or init.lua?

If i did it in the milk.lua, i could only reference the milk entity, in turn, not making the respawn select one of the two at random. And do somehow reference all of the info in the init is beyond me. A hook perhaps? If so, i need an example. Thanks for the response.

I’ll continue to play around with this, if i get it working i’ll let you know. If not, well then… I guess i’ll be waiting on another response from you. xD

I solved the issue with very over-complex code.

I always struggle with the simple stuff in lua, yet i do very well with stuff that takes longer and more effort. Meh. Thanks anyhow!