Wait() or task.wait() cancels thread execution

Hello developers, I’m having an issue with threads where wait() and task.wait() both cancels thread execution. I’ve never seen this issue nor experienced it myself, so thank you in post.

Code:

return function(Player: Player, Message: string)
	local t = coroutine.create(function()
		local Gui = Player.PlayerGui.WarnGui.Warning:Clone()
		Gui.Parent = Player.PlayerGui.WarnGui
		Gui.Text = ("Notification: %s"):format(Message)
		Gui.Name = Message

		local Sfx = Sounds.PaperWrite2:Clone()
		Sfx.Parent = Gui
		Sfx:Play()

		warn("fading in")
		TweenService:Create(Gui, TweenInfo.new(0.25), { TextTransparency = 0 }):Play()
		
		task.wait(3) -- Cancels thread execution
		
		warn("fading out")
		TweenService:Create(Gui, TweenInfo.new(0.25), { TextTransparency = 1 }):Play()
		
		task.wait(0.5)
		
		warn("destroying")
		Sfx:Destroy()
		Gui:Destroy()
	end)
	coroutine.resume(t)
end

Notes:

  • “fading in” prints out, after that nothing gets executed
  • I’ve tried task.spawn() and spawn()
2 Likes

I dont see anything weird, may you show where the function gets fired overall? maybe it could be related to there

2 Likes

It’s a module script called from the server as you can see, it worked before and now it doesn’t. I’m starting to believe that this is an engine bug.

2 Likes

I recommend trying it outside of module script to see what happens and see if it works or not, just in case

2 Likes

Could be an engine bug, I tried to reproduce the issue but it works fine for me.

2 Likes

I tried executing the thread outside the module script, still doesn’t work.

2 Likes

I think there’s something wrong related to module script as in engine bug, could be me though because it should work.

I think the engine bug is related to module scripts in short

(nevemind)

3 Likes

wait wait wait

can you define tweenservice INSIDE the function? it might work maybe because we dont see tweenservice getting defined there (I have reasons for you to define it inside the function btw just do it!!)

even if it’s defined outside of function it might work why? I have no idea just got a feeling

2 Likes

Nope, didn’t do it
character limit

2 Likes

probably engine bug, no idea what’s wrong
maybe try outside studio? I actually have no clue

2 Likes

Maybe there is something wrong with the Gui variable?

*edit my bad forgot that it cancels at task.wait

2 Likes

I’m pretty sure that’s not the issue, because the first tween works as usual

3 Likes

It may have to do with the fact that a modulescript only runs once, so there’s conflicting code.

“ModuleScripts run once and only once per Lua environment and return the exact same value for subsequent calls to require.”

“Note that the first call to require on a ModuleScript will not yield (halt) unless the ModuleScript yields (e.g. calls task.wait()). The current thread that called require will yield until a ModuleScript returns a value.”

2 Likes

I tried this as a standalone function inside a regular script, gave the same results.

2 Likes

Probably an engine bug, try restarting studio.

Works for me:

I’m pretty sure that won’t do anything because I’ve restarted studio many times since this issue came up.

2 Likes

Did it work before, like did it stop working suddenly?

1 Like
return function(Player: Player, Message: string)
	task.spawn(function()
	    local Gui = Player.PlayerGui.WarnGui.Warning:Clone()
		Gui.Parent = Player.PlayerGui.WarnGui
		Gui.Text = ("Notification: %s"):format(Message)
		Gui.Name = Message

		local Sfx = Sounds.PaperWrite2:Clone()
		Sfx.Parent = Gui
		Sfx:Play()

		warn("fading in")
		TweenService:Create(Gui, TweenInfo.new(0.25), { TextTransparency = 0 }):Play()
		
		task.wait(3) -- Cancels thread execution
		
		warn("fading out")
		TweenService:Create(Gui, TweenInfo.new(0.25), { TextTransparency = 1 }):Play()
		
		task.wait(0.5)
		
		warn("destroying")
		Sfx:Destroy()
		Gui:Destroy()
	end)
end

It was slightly different before, like the GUI wasn’t cloned, etc. but nothing that would effect the thread. (Basically the same in terms of strcutre)

I’ve stated this before.
char limit