Warning "task.wait should not be called on a thread that is already 'waiting' in the task library"

I made a backpack script as a commission, and it was working fine, but now I get this warning
image

Here is the code in question:

local function UpdateEquippedToolFunction()

	while true do 
		local Character = Player.Character
		while not Character or not Character.Parent do task.wait() Character = Player.Character end
		
		local Humanoid : Humanoid = Character:WaitForChild("Humanoid",5)
		if not Humanoid then continue end

		if EquippedTool then
			RemoveToolFromCharacter(Humanoid, EquippedTool)
		end

		if SelectedSlot then 
			if Humanoid.Health == 0 then continue end

			local ToolData = ToolsData[SelectedSlot]
			if not ToolData then continue end

			local Tool = ToolData.Tools[1]

			AddToolToCharacter(Humanoid, Tool)
		end

		coroutine.yield()
	end
end

local UpdateEquippedTool = coroutine.wrap(UpdateEquippedToolFunction)

Having the task.wait() inside the coroutine.wrap seems to be messing it up. I also got an infinite loop (from either while loops, I assume the one waiting for Character) once, but not all the time. This would suggest the warning is actually affecting how the code runs?

I am very confused by this warning, it doesn’t even make sense to me and I believe the code is valid… I tried replacing the while loop with the task.wait with Player.CharacterLoaded:Wait() but it returned nil?

Does anyone know what this warning means and what is wrong with my code

I just noticed I got some continue that could be the cause of the infinite loop. I will look into that

1 Like

I managed to get rid of of task.wait() simply by not running it if there is no character, since the function is called when the character is loaded
However, I am still very confused about that warning