#Trying to make an enemy/monster spawn by myself, is there anything wrong with this script?

1 messages · Page 1 of 1 (latest)

torn vault
#

ocal Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")
local enemyModel = ServerStorage:FindFirstChild("Drooling Zombie") -- Ensure the correct model name
local spawnLocations = {
Vector3.new(0, 10, 0),
Vector3.new(50, 10, 50),
-- Add more locations as needed
}

local function spawnEnemy(location)
if enemyModel then
local enemyClone = enemyModel:Clone()
enemyClone.Parent = Workspace
local primaryPart = enemyClone:FindFirstChild("HumanoidRootPart")
if primaryPart then
primaryPart.CFrame = CFrame.new(location)
end
return enemyClone
end
return nil
end

local function isLocationOccupied(location)
for _, character in Workspace:GetChildren() do
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
local rootPart = humanoid.RootPart
if rootPart then
local distance = (location - rootPart.Position).Magnitude
if distance < 10 then
return true
end
end
end
end
return false
end

local function spawnEnemies()
for _, location in ipairs(spawnLocations) do
if not isLocationOccupied(location) then
spawnEnemy(location)
end
end
end

while true do
spawnEnemies()
task.wait(math.random(3, 10)) -- Wait for a random interval between 3 and 10 seconds
end

olive zealot
#

format code like this^

torn vault
#

i forgot to indent the code lol

olive zealot
#

the script editor can do it for you automatically

torn vault
#

yep thanks it's working now