Hello all,
I am working on a spawning system for enemies in my game. I am using a coroutine.wrap
function to spawn enemies at the same time. The coroutine function is being called but none of the code inside it is actually running. I put some break points and used print()
to see if my code is running, which it is. I’m not great at coroutine functions so if I could get some help on this it would be great.
Spawning enemies function:
local function spawnEnemies(character)
local BasicenemyCount = math.random(1,3)
print('b')
for i = 1, BasicenemyCount do
local Basicenemy = enemyFolder:FindFirstChild("Enemy"..math.random(1,3)):Clone()
local basePos = character.HumanoidRootPart.Position
local raycastorigin = basePos + Vector3.new(math.random(-30,30),150,math.random(-30,30))
local raycastDirection = Vector3.new(0,-500,0)
local rayparams = RaycastParams.new()
rayparams.FilterType = Enum.RaycastFilterType.Exclude
rayparams.FilterDescendantsInstances = {}
local raycastresult = game.Workspace:Raycast(raycastorigin,raycastDirection,rayparams)
if raycastresult then
local spawnpart = Instance.new("Part")
local spawnParticles = Instance.new("ParticleEmitter")
--Cosmetic spawning effect
local function createParticles()
spawnpart.Size = Vector3.new(1,1,1)
spawnpart.Transparency = 1
spawnpart.Position = raycastresult.Position+Vector3.new(0,5,0)
spawnpart.Anchored = true
spawnpart.CanCollide = false
spawnpart.CanQuery = false
spawnpart.CanTouch = false
spawnParticles.Parent = spawnpart
local color = {
ColorSequenceKeypoint.new(0,Color3.fromRGB(255,170,0)),
ColorSequenceKeypoint.new(1,Color3.fromRGB(255,170,0)),
}
spawnParticles.Color = ColorSequence.new(color)
spawnParticles.LightEmission = 1
spawnParticles.LightInfluence = 1
local sizepoints = {
NumberSequenceKeypoint.new(0, 0), -- At t=0, size of 0
NumberSequenceKeypoint.new(0.15, 0.25), -- At t=1, size of 10
NumberSequenceKeypoint.new(0.5, 0.25), -- At t=1, size of 10
NumberSequenceKeypoint.new(1, 0), -- At t=1, size of 10
}
spawnParticles.Size = NumberSequence.new(sizepoints)
spawnParticles.Texture = "rbxassetid://18757044141"
local Tpoints = {
NumberSequenceKeypoint.new(0, 1), -- At t=0, size of 0
NumberSequenceKeypoint.new(0.15, 0.2), -- At t=1, size of 10
NumberSequenceKeypoint.new(0.75, 0.25), -- At t=1, size of 10
NumberSequenceKeypoint.new(1, 1), -- At t=1, size of 10
}
spawnParticles.Transparency = NumberSequence.new(Tpoints)
spawnParticles.Lifetime = NumberRange.new(2,4)
spawnParticles.Rate = 30
spawnParticles.Speed = NumberRange.new(5,7)
spawnParticles.SpreadAngle = Vector2.new(-180,180)
spawnParticles.Enabled = true
end
createParticles()
spawnpart.Parent = workspace
coroutine.wrap(function()
--Code inside here doesn't run (except for print())
task.wait(2)
spawnParticles.Enabled = false
task.wait(0.5)
print('c')
Basicenemy:SetPrimaryPartCFrame(spawnpart.CFrame)
Basicenemy.Parent = NPCFolder
spawnpart:Destroy()
raycastresult = nil
print('d')
end)()
end
end
task.wait(math.random(3,6))
end
Main game loop:
RunService.Heartbeat:Connect(function(deltatime)
if canSpawnEnemies then
for i, character in pairs(characterFolder:GetChildren()) do
canSpawnEnemies = false
spawnEnemies(character)
task.wait(5,10)
canSpawnEnemies = true
end
end
end)