Coroutine Issues

So i have been making a spawning zombies wave system for the past hours. And im getting an issues which frustrate me.

Issues: ServerScriptService.Waves:80: missing argument #1 to ‘wrap’ (function expected). I will highlight which line is that. (This is a module script)
Yet if i remove the coroutine.wrap in the Move function it wont spawn more than 1 zombies, they’re stuck but doesn’t yield any error

local enemies = game:GetService("ReplicatedStorage").Enemies
local wave = {}

wave.Wave = 0

function wave:Move(enemy)
	local enemy = enemy
	local zombietorso = enemy:WaitForChild("Torso")
	local zombiehumanoid = enemy:WaitForChild("Humanoid")
	local enemyAnimation = enemy:WaitForChild("Animations")

	local function findtarget()
		local agrodistance = math.huge
		local target = nil
		for i, v in pairs(game.Workspace.Unit:GetChildren()) do
			local human = v:FindFirstChild("Humanoid")
			local torso = v:FindFirstChild("Torso")
			if human and torso and v ~= script.Parent then

				if (zombietorso.Position - torso.Position).magnitude < agrodistance then
					agrodistance = (zombietorso.Position - torso.Position).magnitude
					target = torso
				end
			end
		end
		return target
	end

	coroutine.wrap(function()
		while task.wait() do
			local torso = findtarget()
			if torso then
				zombiehumanoid:MoveTo(torso.Position)
			else
				zombiehumanoid:MoveTo(workspace.Goal.Position)
			end
		end
	end)()
	
end

function wave:Create(data)
	wave.Wave += 1
	local endedEvent = Instance.new("BindableEvent")
	local waveEnemies = Instance.new("Folder")
	waveEnemies.Name = tostring(wave.Wave)
	waveEnemies.Parent = workspace.Enemies
	waveEnemies.ChildRemoved:Connect(function()
		if #waveEnemies:GetChildren() == 0 then
			endedEvent:Fire()
		end
	end)
	endedEvent.Event:Connect(function()
		waveEnemies:Destroy()
	end)
	for enemyType, amount in pairs(data) do
		local enemy = enemies:FindFirstChild(enemyType)
		for i = 1, amount do
			task.wait(1)
			local clone = enemy:Clone()
			clone:SetPrimaryPartCFrame(CFrame.new(math.random(18,50), 2.115, 13.249))
			clone.Parent = waveEnemies
			coroutine.wrap(wave:Move(clone))() -- This Line
		end
	end
	local currentWave = {Ended = endedEvent.Event}
	function currentWave:End()
		endedEvent:Fire()
	end
	return currentWave
end

return wave

Replace that line with this:

coroutine.wrap(wave:Move, clone)()

well that still doesnt work
it gives me Attempt to index nil with Waitforchild

And if i remove the while task.wait as i said in the post it will works but it wont spawn more zombies or just one. Im suspecting is it the loop that cause it?

You’re not using the correct syntax which is:
coroutine.wrap(wave.Move)(wave, clone)

wave:Move with a colon is a method which is slightly different than a normal function which is why you were getting the error

few things 1 in your move coroutine add a break like so because if you don’t you’ll cause memory leaks

while task.wait() do
  if zombiehumanoid.Health <= 0 or zombiehumanoid.Died then
   break;
  end
end

and either remove the while loop coroutine entirely or remove the wave:Create coroutine you don’t need 2 coroutines per enemy. If you keep the wave:Create coroutine change it to

coroutine.wrap(function() wave:Move(clone) end)();

and every time you make a wave you add a few connections that’s never disposed of, if you have like 20 waves or so it’ll get pretty redundant so either disconnect them when they run or put them outside the function.

2 Likes

Yeah,what you’ve been saying should help!

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