Hey all.
I’ve been trying to debug one of my modules (for a push notification UI), and I’ve been seeing really strange behavior from it.
So basically thread manipulation isn’t working inside of the module for certain cases.
Functions such as spawn
and coroutine.wrap/create
are literally just ignored. When I ran my module line by line in the debugger, I found that spawn
and the likes are just skipped over.
In most other cases, however, they work as expected.
Here’s the relevant code and an explaination of it.
NotificationController Module
This module is what handles the managing of push notifications.----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- @Name : UnmuteNotifications
-- @Description : Un-mutes notifications.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
function NotificationController:UnmuteNotifications()
Notifications_Muted=false
self:PushNotification(
"Notifications",
"Notifications have been un-muted."
)
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- @Name : PushNotification
-- @Description : Creates a push notification with the given title, prompt, color and UI.
-- @Params : string "Title" - The title of the notification.
-- string "Prompt" - The prompt of the notification.
-- optional string "Color" - The color of the notification.
-- optional Instance <ScreenGui> "UI" - The UI to display when the notification is clicked.
-- optional function "Func" - The function to run when the notification is clicked.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
function NotificationController:PushNotification(Title,Prompt,Color,UI,Func)
if not Notifications_Muted then
warn("Push Notification")
Notification_UI:ShowNotification(Title,Prompt,Color)
end
end
Notification_UI Module
This module handles the displaying and interaction of notifications. The spawn() in question is in this module.local Notification_Display_Time=5 --Time for a notification to display before hiding and being archived.
local Notification_UI=game.ReplicatedStorage.UIs.Notification_UI:Clone()
local BaseNotification=Notification_UI.Notification
function NotificationUI:ShowNotification(Title,Prompt,Color)
warn("Create Notification")
local Notification=BaseNotification:Clone()
Notification.Name="_Notification"
Notification.Title.Text=Title
Notification.Prompt.Text=Prompt
Notification.ImageColor3=(Color or Notification.ImageColor3)
Notification.Position=UDim2.new(1.3,0,0.7,0)
Notification.Parent=Notification_UI
script.Ding:Play()
Notification:TweenPosition(UDim2.new(1,0,0.7,0),"Out","Sine",0.5)
spawn(function() --THIS DOESNT RUN SOMETIMES!?
wait(Notification_Display_Time)
Notification:TweenPosition(UDim2.new(1.3,0,0.7,0),"Out","Sine",0.5)
wait(0.5)
Notification:Destroy()
end)
return
end
LobbyController Module
The function in here is ran when the player enters the lobby from either joining the game or leaving a round.----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- @Name : ShowLobbyUI
-- @Description : Shows the lobby UI
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
function LobbyController:ShowLobbyUI()
Lobby_UI:Show() --Showing the lobby UI
StarterGui:SetCore("SetAvatarContextMenuEnabled",true)
self.Controllers.NotificationController:UnmuteNotifications()
end
Code from localscript that ties the modules together
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- @Name : LoadController
-- @Description : Loads the specified controller module into the engine.
-- @Params : Instance <ModuleScript> "ControllerModule" - The controller module to load into the engine
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
function DragonEngine:LoadController(ControllerModule)
local ControllerName=ControllerModule.Name
local Controller;
local s,m=pcall(function() --If the module fails to load/errors, we want to keep the engine going
Controller=require(ControllerModule)
end)
if not s then --Controller module failed to load
DragonEngine:Log("Failed to load Controller '"..ControllerName.."' : "..m,"Warning")
else --Controller moduled was loaded
setmetatable(Controller,{__index=DragonEngine}) --Exposing Dragon Engine to the controller
DragonEngine.Controllers[ControllerName]=Controller
DragonEngine:DebugLog("Loaded Controller '"..ControllerName.."'.")
end
end
DragonEngine:DebugLog("Loading and initializing controllers...")
for _,ControllerModule in pairs(Recurse(Paths.Controllers)) do
if ControllerModule:IsA("ModuleScript") and ControllerModule:FindFirstChild("Ignore")==nil then
DragonEngine:LoadController(ControllerModule)
local Controller=DragonEngine:GetController(ControllerModule.Name)
if type(Controller.Init)=="function" then
DragonEngine:DebugLog("Initializing controller '"..ControllerModule.Name.."'...")
Controller:Init()
end
end
end
DragonEngine:DebugLog("Starting controllers...")
for ControllerName,Controller in pairs(DragonEngine.Controllers) do
if type(Controller.Start)=="function" then
DragonEngine:DebugLog("Starting controller '"..ControllerName.."'...")
coroutine.wrap(Controller.Start)(Controller) --Starting the controller in its own thread, while giving it
--direct access to itself
end
end
As you can see, NotificationController.UnmuteNotifications
is called from the lobby controller when the lobby UI is showing. This is to enable notifications while in the lobby, but to mute them while in a round.
For whatever reason, in that case spawn
doesn’t work. There is no errors or warnings, spawn
is literally just skipped over.
But if I call NotificationController.UnmuteNotifications
from a different module other than the LobbyController
module, spawn
works as expected.
I have honestly no clue what’s causing this.
What’s the issue with the code? spawn
works perfectly fine in all of my other modules, it’s just this one specific module that’s giving me the odd behavior.