diff --git a/src/client/controllers/welcome-chat-controller.ts b/src/client/controllers/welcome-chat-controller.ts index 203cc14..3c0bfe0 100644 --- a/src/client/controllers/welcome-chat-controller.ts +++ b/src/client/controllers/welcome-chat-controller.ts @@ -6,10 +6,14 @@ import { WELCOME_CHAT, WELCOME_CHAT_MESSAGE } from "shared/welcome-chat"; @Controller() export class WelcomeChatController implements OnStart { public onStart(): void { - task.delay(WELCOME_CHAT.delaySeconds, () => this.displayWelcome()); + task.spawn(() => this.displayWelcome()); } private displayWelcome(): void { + if (!game.IsLoaded()) { + game.Loaded.Wait(); + } + const deadline = os.clock() + WELCOME_CHAT.channelTimeoutSeconds; const channels = TextChatService.WaitForChild( "TextChannels", @@ -22,6 +26,26 @@ export class WelcomeChatController implements OnStart { return; } + // Channels can exist before the chat UI subscribes to system messages. + const window = TextChatService.WaitForChild( + "ChatWindowConfiguration", + math.max(deadline - os.clock(), 0), + ); + if (!window?.IsA("ChatWindowConfiguration")) { + Log.Warn("Welcome chat unavailable: the chat window configuration is missing"); + return; + } + while (window.AbsoluteSize.X <= 0 || window.AbsoluteSize.Y <= 0) { + if (os.clock() >= deadline) { + Log.Warn( + "Welcome chat unavailable: the chat window did not become ready before the timeout", + ); + return; + } + task.wait(0.1); + } + + task.wait(WELCOME_CHAT.delaySeconds); channel.DisplaySystemMessage(WELCOME_CHAT_MESSAGE); } }