Well, an enemy has a function that must generate 1 secondary enemy each time, this function is executed in a loop, and is inside a Task.Spawn so that it does not interfere with the rest of the script, so every 20 seconds it will execute an animation in which when it reaches a specific event it will spawn the secondary enemy
Well the problem is that every time the function is executed, more and more enemies are generated. For example, in the first execution, this will only spawn 1 enemy, as it should be, in the second execution 2 will spawn, that is where the problem begins, and so on, in the third 3 will spawn, in the fourth 4 will spawn. And that is not should happen, in the end it ends up spawning more than 50 at a time
Well, I can’t find a bug, because it even has a Print to test it, and the Print is only executed once every time
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local isSpawning = folderValues.Extras:WaitForChild("IsSpawning")
local startSpawning = folderValues.Extras:WaitForChild("StartSpawning")
task.spawn(function()
local function20
function20 = startSpawning.Changed:Connect(function()
while task.wait(20) do
if isSpawning.Value == false then
print("Spawning Teram-02")
isSpawning.Value = true
--script.Disabled = true
humanoid.WalkSpeed = 0
SpawnAnimation:Play()
SpawnAnimation:GetMarkerReachedSignal("Shoot"):Connect(function()
local SpawnEnemy = ServerStorage.EnemiesLibrary.V3:WaitForChild("Teram-02"):Clone()
SpawnEnemy.Parent = workspace.Enemies
SpawnEnemy.PrimaryPart.CFrame = script.Parent.HumanoidRootPart.CFrame * CFrame.new(0, 0, -5)
local cloneSound = folderSounds.Sounds:WaitForChild("TeramSpawning"):Clone()
cloneSound.Parent = hrp
cloneSound:Play()
cloneSound.Stopped:Connect(function()
cloneSound:Destroy()
end)
end)
wait(SpawnAnimation.Length)
isSpawning.Value = false
SpawnAnimation:Stop()
humanoid.WalkSpeed = WalkSpeedValue.Value
--script.Disabled = false
function20:Disconnect()
end
end
end)
startSpawning.Value = true
end)