LurrrchJoin Date: 2009-04-13 Post Count: 1079 |
I'm trying out coroutines in my latest project, and I'm a little confused. Yield, in its basic form, seems like such a simple thing... it's supposed to pause the coroutine, no? But when I run this...
local backupface = coroutine.wrap(function()
local character = player.Character
print("Character is " .. character.Name .. " for backupface")
character.Head:WaitForChild("face").Parent = nil
print("Backupface complete")
coroutine.yield()
wait(1)
print("cour resumed")
end)
...the entire thing runs, including the print at the end. "Backupface" is only being called once, but the yield seems to be completely ignored and it just goes through regardless.
I'm probably missing something really obvious here; this is my first time touching these. Any help is welcome! |
|
chimmihcJoin Date: 2014-09-01 Post Count: 17143 |
Look at this:
x = coroutine.wrap(function()
print(1)
coroutine.yield()
print(2)
end)
x() --> 1
x() --> 2
x() --> cannot resume dead coroutine |
|
LurrrchJoin Date: 2009-04-13 Post Count: 1079 |
?
I know it cannot resume when dead, but I'm saying that it's ignoring the yield completely. It should be able to "resume" twice, one for the initial run and one that would do the final print after the yield, but it does it all in one run.
If your example ran with the same problem I'm having, it would print 1 and 2 in the same go. |
|
LurrrchJoin Date: 2009-04-13 Post Count: 1079 |
Basically my coroutine.yield() is not yielding and I'm totally confused as to why that is. |
|
chimmihcJoin Date: 2014-09-01 Post Count: 17143 |
There is no problem with the code in the first post, it runs as expected.
--
local player = game.Players.LocalPlayer
local backupface = coroutine.wrap(function()
local character = player.Character
print("Character is " .. character.Name .. " for backupface")
character.Head:WaitForChild("face").Parent = nil
print("Backupface complete")
coroutine.yield()
wait(1)
print("cour resumed")
end)
backupface()
--
output:
Character is Player for backupface
Backupface complete |
|
LurrrchJoin Date: 2009-04-13 Post Count: 1079 |
And it didn't also print "cour resumed" in that same run??? Why is it doing it for me? This is odd. |
|
|
You're not calling it multiple times, are you? |
|
LurrrchJoin Date: 2009-04-13 Post Count: 1079 |
That's what my first thought was, but no, I don't see any way that could be happening. It's called when the player's character is added, and that's the only way it can be. |
|
|
Can you give us the code where that actually happens? |
|
LurrrchJoin Date: 2009-04-13 Post Count: 1079 |
player.CharacterAdded:connect(function()
runit()
end)
Just tested it with debounces to see if it was firing twice somehow. It definitely wasn't - fires once, still calls coroutine twice. |
|
|
It's has to be being called twice somewhere. Is runit a separate function? |
|
LurrrchJoin Date: 2009-04-13 Post Count: 1079 |
Yes, and I just found the part that makes it run through. When I delete this section of code in "runit," the yield works:
local human = character:findFirstChild("Humanoid")
if human ~= nil then -- making human invisible
character:findFirstChild("Head").Transparency = 1
character:findFirstChild("Torso").Transparency = 1
character:findFirstChild("Left Arm").Transparency = 1
character:findFirstChild("Right Arm").Transparency = 1
character:findFirstChild("Left Leg").Transparency = 1
character:findFirstChild("Right Leg").Transparency = 1
if character:findFirstChild("Head"):findFirstChild("face") == nil then return end
character:findFirstChild("Head"):findFirstChild("face"):remove()
if character:findFirstChild("Torso"):findFirstChild("roblox") == nil then return end
character:findFirstChild("Torso"):findFirstChild("roblox"):remove()
end
If I put that section back in, the yield is skipped/the coroutine is run twice. Why? |
|
|
Maybe when you removed one of those things from the character model, the character gets reloaded? That could explain having the event being triggered twice. |
|
LurrrchJoin Date: 2009-04-13 Post Count: 1079 |
Yup, I removed the removals and it's working now. I have no idea why that would work like that. But now I have a new problem... Everything loads without error or issue and the yield stays, but the face is just straight up not being removed now. The coroutine is set to wait for and remove the face decal, but it's managing to print "Backupface complete" when the face is clearly still there. What. |
|
|
Try this:
if character:FindFirstChild("Humanoid") then
character:FindFirstChild("Head").Transparency = 1
character:FindFirstChild("Torso").Transparency = 1
character:FindFirstChild("Left Arm").Transparency = 1
character:FindFirstChild("Right Arm").Transparency = 1
character:FindFirstChild("Left Leg").Transparency = 1
character:FindFirstChild("Right Leg").Transparency = 1
if character:FindFirstChild("Head"):FindFirstChild("face") then
character.head.face:Destroy()
end
if character:FindFirstChild("Torso"):FindFirstChild("roblox") then
character.Torso.roblox:Destroy()
end
end |
|
|
Oops, forgot to capitalize head. Use this instead.
Try this:
if character:FindFirstChild("Humanoid") then
character:FindFirstChild("Head").Transparency = 1
character:FindFirstChild("Torso").Transparency = 1
character:FindFirstChild("Left Arm").Transparency = 1
character:FindFirstChild("Right Arm").Transparency = 1
character:FindFirstChild("Left Leg").Transparency = 1
character:FindFirstChild("Right Leg").Transparency = 1
if character:FindFirstChild("Head"):FindFirstChild("face") then
character.Head.face:Destroy()
end
if character:FindFirstChild("Torso"):FindFirstChild("roblox") then
character.Torso.roblox:Destroy()
end
end |
|
LurrrchJoin Date: 2009-04-13 Post Count: 1079 |
Agh, this is weird.
I can keep the removes out of that section, that's fine, I didn't need them there anyway. But now I've got this:
local backupface = coroutine.wrap(function()
-- while true do
print("cour began")
local character = player.Character
print("Character is " .. character.Name .. " for backupface")
-- character.Head:WaitForChild("face").Parent = nil
character:WaitForChild("Head")
local face = character.Head:WaitForChild("face")
if face ~= nil then face:Destroy()
end
print("Backupface complete")
coroutine.yield()
wait(1)
print("cour resumed")
-- end
end)
And it runs without error or skipping the yield. But it just doesn't destroy the face! Even if I remove the face ~= nil part, making it so that it attempts to destroy the face no matter what, it still runs like it did destroy it without error but the face decal is clearly still there. Why?!? |
|