I'm trying to make an entity spawn more than once using a table of vectors, but I also want the entity to be able to be interacted with i.e: if you touch it the entity removes itself.
And I want the entity to keep spawning randomly around the vectors even after a player has picked one of them up. Here's what I have but it throws a NULL entity error even with a validity check. I'm aware it's either because the timer doesn't know what the entity is and/or the entity has already been removed I just don't know how to fix it:
being called init.lua
concommand.Add( "spawntrophs", function()
local t = 0
local interval = 5
local spawnPos = {
Vector(-810.278809, 850.462097, -12223.968750),
Vector(790.265930, 775.635620, -12223.968750),
Vector(751.735107, -783.828308, -12223.968750),
Vector(-815.747437, -719.911682, -12223.968750),
Vector(-154.760635, -79.551308, -12223.968750),
}
hook.Add("Think","Time2Move", function()
if t < CurTime() then
t = CurTime() + interval
-- single player?
local scav = ents.Create("scav_common")
if IsValid(scav) then
scav:SetPos(spawnPos[math.random(1, table.Count(spawnPos))])
scav:SetName("scavitem1")
scav:Spawn()
timer.Simple(10,function() scav:Remove() end)
end
end
end)
Also I know this probably isn't the best way to do what I'm wanting to but again I don't know how else to do it.
scratch the above - I realized it was extremely unnecessary to do a lot of it:
AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")
include("shared.lua")
resource.AddFile("addons/darkrpmodification-master/sound/booyah.mp3")
local cooldown = 30
local t = 0
local interval = 5
local spawnPos = {
Vector(-810.278809, 850.462097, -12223.968750),
Vector(790.265930, 775.635620, -12223.968750),
Vector(751.735107, -783.828308, -12223.968750),
Vector(-815.747437, -719.911682, -12223.968750),
Vector(-154.760635, -79.551308, -12223.968750),
}
function ENT:Initialize()
util.PrecacheSound("addons/coin_pickup/sound/Lemons.wav")
self:SetModel("models/props_c17/doll01.mdl")
self:SetMaterial("models/shiny")
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_NONE)
self:SetSolid(SOLID_VPHYSICS)
self:SetColor(Color(255,221,0))
self:SetPos(spawnPos[math.random(1, table.Count(spawnPos))])
local phys = self:GetPhysicsObject()
if (phys:IsValid()) then
phys:Wake()
self:SetCollisionGroup(COLLISION_GROUP_WEAPON)
self:SetTrigger(true)--start touch events or others
self:DrawShadow(false)
self.PlyDelays = {}
end
end
function ENT:PhysgunPickup(ply,ent)
if ply then return end
end
function ENT:GravGunPickupAllowed(ply,ent)
if ply then return end
end
function ENT:Touch(e)
local curtime = CurTime()
if !e:IsPlayer() then return end
local ID = e:GetCreationID()
if self.PlyDelays[ID] and self.PlyDelays[ID] > curtime then return end
self.PlyDelays[ID] = curtime + cooldown
if e:IsPlayer() and e:Team() == TEAM_HOBO then self:EmitSound("booyah.mp3")
e:addPocketItem(self) else e:SendLua("chat.AddText(Color(255,0,0), 'You are not the right job!')") return
end
end
function ENT:Think()
self:CallOnRemove("respawn", function() local ent = ents.Create("scav_common") ent:SetPos(spawnPos[math.random(1, table.Count(spawnPos))]) ent:Spawn() end)
end
all I need to do now is spawn more than one of these and delay its respawn.
Can someone guide me?
Have the CallOnRemove function inside the Initialize hook. Also try table.Random to fetch the position from the table.
If that doesn't work try using ENTITY/OnRemove instead of CallOnRemove
Sorry, you need to Log In to post a reply to this thread.