Wait() and using OnIncomingMessage is causing my studio/game to crash

I have a function in the client script that calls a fireToast() function. this fireToast accepts a toast message and tween’s a GUI. Without the wait function, the code works completely fine and there are no crashes but once I uncomment it it goes back to crashing. I literally don’t see how this is happening.

local function FireToast(toastMessage) 
	wait(5)
end

I can’t tell whether this is a bug or whether it is something else, so I’ve just put it into scripting support. I can’t see this being anything wrong with my code, though.

EDIT: I’ve edited my code, I actually commented the whole thing out and just added the wait and I’m still getting the crashes. It crashes once the wait is completed, so if I set a wait for 10 seconds it will crash after the 10 seconds.

5 Likes

What is calling the FireToast function? What is that script?

1 Like

Soo… It crashes as soon as the function ends? Where do you call the FireToast function and what happens after it?

It’s from the script itself, the script is is StarterPlayerScripts. Nothing happens after the FireToast has been called, it’s just nested in an if statement. I don’t see how this would cause the whole of studio to crash, though.

I just meant like what does the code look like? Something about the delay is causing problems, it’s probably not to do with the wait() itself. Try task.wait() too, it’s the newer and better one.

if (roomPlayer == false and roomExists ~= nil) then
					print("More than one player has been found")						
					local warningToastMessage = "More than one player has been found."
					FireToast(warningToastMessage)
				elseif (roomPlayer == 0 and roomExists ~= nil) then
					print("No players have been found")
						
					local warningToastMessage = "No players have been found."
					FireToast(warningToastMessage)
				elseif (roomExists == nil) then
					local warningToastMessage = "Room " .. enteredNumber .. " doesn't exist"
					FireToast(warningToastMessage)
				else
					RoomGiveEvent:FireServer(roomPlayer, enteredNumber)
				end

I’ll just provide the whole of the code, neither of the FireToasts that are called actually work. (Sorry about the formatting)

I also tried task.wait(3) and I’m still crashing.

1 Like

This is a little unrelated but I want to point out that you should be using task.wait() as wait() is deprecated.

Ok, that doesn’t seem like the entire code. You should also check your client’s memory usage, is it going up like crazy, and is the crash instant?

It was just the code where the FireToast is found, the rest of the event is unrelated. The crash happens as soon as the task.wait() finishes, if I change the time to 10 or 20 seconds it will crash after 10 or 20 seconds.

The main reason it would crash is not because anything is wrong with waiting, no. Something is probably running after this if statement ended, as he stated, the crash happens after the wait.

So what if in the code where firetoast is found, you comment it out and replace it with the wait right there?

Also it probably is something outside of that if nest.

I assume by crashing you mean Studio times out. If the crash is caused directly after you call wait then there could easily be another script unrelated causing this (because lua threading is interesting)

also mandatory please stop using wait post

I tried removing it out the function and still got the crash which is odd. I don’t get how waiting would cause a crash, this is the end of the logic from the client as all it does is activates a GUI and tweens it. I’m using TextChatService.OnIncomingMessage and splitting the message, whether that has anything to do with it but it doesn’t seem like it would because all other logic works up until the point of the if statement (which I put in the post). I’ll have a look around and see If i can find anything.

for reference,

TextChatService.OnIncomingMessage = function(message)
	if (message.Status ==  Enum.TextChatMessageStatus.Success) then
		if (message.TextSource.Name == player.Name) then

It’s a callback connection, do you return anything at the end of your function? Try adding empty return at the end. That might be the problem.

Added a return nil, still crashes after the three seconds on the task.wait()

:slightly_frowning_face:

How about you use something like… MessageReceived connection instead of OnIncomingMessage callback?
In the documentation it states that OnIncomingMessage should be only used once in sources, just maybe using it in your script breaks something.

TextChatService.MessageReceived:Connect(function(message)
    if (message.Status ==  Enum.TextChatMessageStatus.Success) then

I have no idea why this works, but it does. Obviously OnIncomingMessage and task.wait() don’t work together, guessing it’s a bug or something to do with the callback, not sure. Thanks though!

Glad it worked out! :raised_hands:
Using it probably infinite loops the default chat scripts and crashes. Try to exclude OnIncomingMessage from using it again. :blush:

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