#Enemy spawn script has errors

1 messages · Page 1 of 1 (latest)

upper crypt
#

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):

#

local alien = script.Parent
local humanoid = alien:FindFirstChildOfClass("Humanoid")
local root = alien:FindFirstChild("HumanoidRootPart")
local Players = game:GetService("Players")

if not humanoid or not root then
warn("Alien missing Humanoid or HumanoidRootPart")
return
end

-- Add AlienIdentifier if it doesn't exist
if not alien:FindFirstChild("AlienIdentifier") then
local id = Instance.new("BoolValue")
id.Name = "AlienIdentifier"
id.Value = true
id.Parent = alien
end

-- Walk animation
local walkAnim = Instance.new("Animation")
walkAnim.AnimationId = "rbxassetid://133068354978213"
local walkTrack = humanoid:LoadAnimation(walkAnim)
walkTrack.Priority = Enum.AnimationPriority.Movement

-- Hit animation
local hitAnim = Instance.new("Animation")
hitAnim.AnimationId = "rbxassetid://95010674088162"
local hitTrack = humanoid:LoadAnimation(hitAnim)
hitTrack.Priority = Enum.AnimationPriority.Action

-- Touch damage
local damageAmount = 10
local debounceTime = 1
local lastHit = {}

local function onTouch(hit)
local character = hit.Parent
if not character then return end

-- Ignore other aliens
if character:FindFirstChild("AlienIdentifier") then return end
#

local playerHumanoid = character:FindFirstChildOfClass("Humanoid")
if playerHumanoid then
local now = tick()
if lastHit[character] and now - lastHit[character] < debounceTime then return end
lastHit[character] = now

    playerHumanoid:TakeDamage(damageAmount)

    if hitTrack.IsPlaying then
        hitTrack:Stop()
    end
    hitTrack:Play()
end

end

-- Connect all parts to touch
for _, part in pairs(alien:GetDescendants()) do
if part:IsA("BasePart") then
part.Touched:Connect(onTouch)
end
end

-- Optional: clean debounce table
spawn(function()
while true do
for char, timeStamp in pairs(lastHit) do
if tick() - timeStamp > debounceTime then
lastHit[char] = nil
end
end
wait(0.5)
end
end)

-- Follow nearest player
local function getNearestPlayer()
local nearestPlayer = nil
local shortestDistance = math.huge
for _, player in pairs(Players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local dist = (root.Position - player.Character.HumanoidRootPart.Position).Magnitude
if dist < shortestDistance then
shortestDistance = dist
nearestPlayer = player
end
end
end
return nearestPlayer
end

-- Death handler
humanoid.Died:Connect(function()
walkTrack:Stop()
hitTrack:Stop()
wait(2)
alien:Destroy()
end)

-- Follow loop
humanoid.WalkSpeed = 16
while humanoid.Health > 0 do
local player = getNearestPlayer()
if player and player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
humanoid:MoveTo(player.Character.HumanoidRootPart.Position)
if not walkTrack.IsPlaying then
walkTrack:Play()
end
end
wait(0.2)
end