Help with spawning enimies at random locations

Hi there i am trying to make a script that will clone a Enemy Bot that i have made to chase me and make it spawn at a random location that have been specified (in RED) but I keep getting this error "Script timeout: exhausted allowed execution time " and not knowing why it is being cause because i have used it in other script like this before

here is the code i have used
‘’’

local part = script.Parent
local spawnZone = game.Workspace.SpawnZone

local ServerStorage = game:GetService("ServerStorage")
local spawns = game.Workspace:WaitForChild("EnemySpawns")
local spawnsTabel = spawns:GetChildren()

local canSpawn = true
local maxSpawnedEnemys = 15 ---Adjust Based on Level or Gamemode

local function spawnZombie(otherPart)
    local humanoid = otherPart.Parent:FindFirstChildWhichIsA("Humanoid")
    local player = game.Players:FindFirstChild(otherPart.Parent.Name)
    local enemyNum = game.Workspace.Enemies:GetChildren()
    if humanoid and player and canSpawn and #enemyNum < maxSpawnedEnemys then
     	canSpawn = false
	local bot = ServerStorage.Enemies["Enemy"]:Clone()
	
	local randomSpawn
	
	repeat
		randomSpawn = spawnsTabel[math.random(1, #spawnsTabel)]
	until not randomSpawn:FindFirstChildWhichIsA(bot)
	
	bot.Parent = randomSpawn
	bot.HumanoidRootPart.CFrame = randomSpawn.CFrame + Vector3.new(0,5,0)
	wait(1)
	canSpawn = true
end
end

part.Touched:Connect(spawnZombie)

‘’’

put a wait() in the repeat function. Because ot runs as fast as while ytue do therefore crashing the script

1 Like

Thank you that works perfectly :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.