Respawn on multiple spawners

  1. What do you want to achieve?
    I would like to add multiple enemy model spawn locations, but I can’t imagine how it would look like ;/.

  2. What is the issue?
    I don’t know how to make a given enemy type respawn on multiple spawners

local spawner = game.Workspace.Level1.Respawns:GetChildren()
local respawnTime = spawner:GetAttribute("RespawnTime")
local enemyName = spawner:GetAttribute("EnemyName")
local enemy : Model = game.ServerStorage.Enemies[enemyName]
local spawnedEnemy : Model = nil

function despawnEnemy()
	if spawnedEnemy then
		spawnedEnemy:Destroy()
		spawnedEnemy = nil
	end
end

function spawnEnemy()
	despawnEnemy()
	local clonedEnemy = enemy:Clone()
	spawnedEnemy = clonedEnemy
	spawnedEnemy.HumanoidRootPart.CFrame = spawner.CFrame + Vector3.new(0,3,0)
	spawnedEnemy.Parent = workspace
end

while true do
	spawnEnemy()
	local humanoid : Humanoid = spawnedEnemy:WaitForChild("EnemyHumanoid")
	humanoid.Died:Wait()
	task.wait(respawnTime)
end
  1. What solutions have you tried so far?
    I’ve read the documentation, I’ve read the forum, but nothing comes to mind
2 Likes

Iterating the spawners table to get each spawnpoint’s CFrame might work If you need to spawn models to multiple spawn locations.

I suggest that you use tables for storing spawned enemies so it’s easier to despawn.
and append instances to the table.

Spawn Locations are manually placed in this example. :grinning:

local spawnedEnemy = {}
table.append(clonedEnemy)
local spawners = game.Workspace.Level1.Respawns:GetChildren()
function spawnEnemy()
	despawnEnemy()
	for spawner in spawners do
		local clonedEnemy = enemy:Clone()
		clonedEnemy = clonedEnemy
		clonedEnemy.HumanoidRootPart.CFrame = spawner.CFrame + Vector3.new(0,3,0)
		clonedEnemy.Parent = workspace
	end 
end

Reach me out if there is a problem.

1 Like

I have a problem because I cannot download the attribute of a particular Respawn

error: attempt to call missing method ‘GetAttribute’ of table (RespawnTime here)

I’m a bit confused why you used “spawners” when I already have the same thing only under the name “spawner”

local spawners = game.Workspace.Level1.Respawns:GetDescendants()
local spawner = game.Workspace.Level1.Respawns:GetChildren()
local respawnTime = spawner:GetAttribute("RespawnTime")
local enemyName = spawner:GetAttribute("EnemyName")
local enemy : Model = game.ServerStorage.Enemies[enemyName]
local spawnedEnemy : Model = {}

function despawnEnemy()
	if spawnedEnemy then
		spawnedEnemy:Destroy()
		spawnedEnemy = nil
	end
end

function spawnEnemy()
	despawnEnemy()
	for spawner in spawners do
		local clonedEnemy = enemy:Clone()
		clonedEnemy = clonedEnemy
		clonedEnemy.HumanoidRootPart.CFrame = spawner.CFrame + Vector3.new(0,3,0)
		clonedEnemy.Parent = workspace
		table.insert(clonedEnemy)
	end 
end

Both spawners and spawner variable returns an array.

local spawners = game.Workspace.Level1.Respawns:GetDescendants()
local spawner = game.Workspace.Level1.Respawns:GetChildren()

error: attempt to call missing method ‘GetAttribute’ of table (RespawnTime here)

probably the attribute you’re calling is in the spawner (maybe I’m wrong.)
can you post the spawner property in the explorer so I can provide a clearer explanation. thanks

Here are my attributes:

image

You know, the only problem is that I’m thinking how separately each respawn is supposed to execute the script to respawn on each respawn the mobs specified in the attribute