Script not yielding for Bindable Function to return

Currently I have a system in where the main script of the game calls another script holding a bindable function, where (I WANT) the main script to yield until the function on the secondary script completes. It is a combat scenario script that spawns in a couple of monsters at specific spot locations, and when all the monsters that were spawned have died, it returns true.

The problem is not the combat code itself, it prints “All Monsters KIlled” exactly when I expect/want it to. From my understanding, I thought that bindable functions yielded scripts until they returned by default.

MAIN SCRIPT (“the battle is ended” is printed as soon as the function is called)

local combatFunction = RepStorage.Combat.CombatFunction

combatFunction:Invoke(game.Workspace.Monsters.Bathroom)
print("The battle has ended")

SECONDARY SCRIPT

local monster = game.ReplicatedStorage.Combat.Monster
combatFunction = game.ReplicatedStorage.Combat.CombatFunction
monstercount = 0
dead = 0

local function startCombat(folder)
	for i,v in pairs(folder:GetChildren()) do
		if v:IsA("Part") then
			local monster = monster:Clone()
			monster.Parent = game.Workspace.Monsters
			monster:SetPrimaryPartCFrame(v.CFrame)
			monstercount = monstercount + 1
			local hum = monster:WaitForChild("Humanoid")
			hum.Died:Connect(function()
				dead += 1
				if dead == monstercount then
					print("All Monsters Dead!")
					dead = 0
					monstercount = 0
					return 
				end
			end)
		end
	end
end

combatFunction.OnInvoke = startCombat

Much love to any that help

SOLVED:

The code was ignoring that there was a return function in the .Died function inside the script I am assuming?

Solution code for second block, the first is fine unchanged :

local monster = game.ReplicatedStorage.Combat.Monster
combatFunction = game.ReplicatedStorage.Combat.CombatFunction
monstercount = 0
dead = 0

local function startCombat(folder)
	for i,v in pairs(folder:GetChildren()) do
		if v:IsA("Part") then
			local monster = monster:Clone()
			monster.Parent = game.Workspace.Monsters
			monster:SetPrimaryPartCFrame(v.CFrame)
			monstercount = monstercount + 1
			local hum = monster:WaitForChild("Humanoid")
			hum.Died:Connect(function()
				dead += 1
			end)
		end
	end
	repeat wait() until dead == monstercount
	print("All Monsters Dead!")
	dead = 0
	monstercount = 0
	return true
end

combatFunction.OnInvoke = startCombat

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