I made a backpack script as a commission, and it was working fine, but now I get this warning
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
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
Sorry for the bump, I noticed this same “issue” happening a week or two ago in a game I am working on.
I’m pretty sure this is a Roblox engine bug, or an oversight on their part when they implemented how the task library handles waiting threads in relation to using the coroutine library.
I can easily reproduce this problem, and I know how it works (at least from a basic understanding).
The following code below will give the same warning that you (and others) have encountered before:
local Thread = coroutine.running()
task.spawn(function()
task.wait() --This is just used for an initial yield to give the parent thread time to process the first task.wait function call
coroutine.resume(Thread)
end)
task.wait(10)
print("thread unyielded")
task.wait(2)
print("Hello!")
It happens when you resume a waiting thread and call task.wait again within the original yield time for the first task.wait call.
It seems that roblox’s internal thread handler doesn’t remove the original “waiting” status of the thread if it’s resumed early.
Currently unable to open any sort of engine bug topic, so for the time being I’ll have to either hope someone with topic creation access can post this for me or wait until I get access to create posts myself.
Do you know if this causes any performance problems from your testing? Is it a bug that only annoys us with warnings, or does it have negative implications for the execution of our code? Currently experiencing this in my places as well…
Not performance, but it can mess with the code. It’s been a while since then, so I don’t remember the exceptions I encountered, but these errors usually prevent the code from working as expected, and the fix is usually to go a little less crazy with threads, coroutines or the task library
I just encountered a similar issue. When using a state machine: Say State A is parked in a task.wait() when the state machine changes to State B. if State B then calls task.wait() it behaves like a loop and returns to the beginning of state B. I thought I was going crazy for a minute
I don’t believe this is a bug, just an internal warning. I’ve had this discussion here that I would recommend reading the entirety of if you have the time:
tl;dr - you probably won’t ever need to use coroutine.create/coroutine.wrap with coroutine.yield. I would recommend using task.spawn and use task.cancel to cancel any running threads instead if needed.