I'm basically trying to make a space game (with my friends) and I came across this issue with the alien spawn script (The enemy spawn script.) The spawn itself works, but the alien npc doesn't. The aliens become immortal, and they seem to attack eachother after the player dies?
AlienSpawn (script in ServerScriptService):
local spawnPoint = workspace:WaitForChild("AlienSpawnPoint") -- the spawn location
local alienTemplate = game.ServerStorage:WaitForChild("Alien") -- your alien model
local spawnInterval = 10 -- seconds between spawns
local maxAliens = 5 -- max aliens at a time
-- Track current aliens
local currentAliens = {}
-- Function to spawn a new alien
local function spawnAlien()
if #currentAliens >= maxAliens then return end
-- Clone alien
local newAlien = alienTemplate:Clone()
newAlien.Parent = workspace
newAlien:SetPrimaryPartCFrame(spawnPoint.CFrame + Vector3.new(0, 3, 0)) -- spawn above ground
-- Ensure humanoid health
local humanoid = newAlien:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Health = humanoid.MaxHealth
-- Remove from currentAliens when dead
humanoid.Died:Connect(function()
for i, a in pairs(currentAliens) do
if a == newAlien then
table.remove(currentAliens, i)
break
end
end
end)
end
table.insert(currentAliens, newAlien)
end
-- Spawn loop
while true do
spawnAlien()
wait(spawnInterval)
end
AlienScript (Inside of Alien model in ServerStorage):