I’ve begun moving handler code from StarterCharacterScripts to ReplicatedStorage because I’ve been seeing that as better practice; one thing I’m overthinking though is how to manage refreshing all the handler’s global variables on CharacterAdded? The only ways I can think of implementing this is to, on CharacterAdded, overwrite all global variables in a new method, or wrap everything up in a method so that all globals are re-initialised for the new character/PlayerGui descendants.
For example, this is how I would cope with creating an input to interact with a ScreenGui right now; (in pseudocode)
local ContextActionService = -- get service
local PlayerGui = -- get player gui
-- Wrap checkpoint; could wrap everything below in a new method,
-- call it on CharacterAdded and it would behave as if it was in StarterCharacterScripts,
-- eliminating the need for the "OnCharacterAdded" module method,
-- but would I need to be wary of memory leaks?
local MyScreenGui; -- initialise the variable globally
--
local module = {}
function module.OnCharacterAdded()
-- fetch the latest version of this asset
MyScreenGui = PlayerGui.MyScreenGui
end
local function OnInput()
-- interact with MyScreenGui here
end
function module.OnHandlerLoad()
-- connect input here
end
The OnCharacterAdded overwrite logic seems really tedious, and I’m not sure if repeatedly recalling all my methods just to re-initialise global variables is good practice; does anyone have any suggestions? Is using StarterCharacterScripts for large projects fine? Or is there a simpler/better way to work with fetching these assets on every character refresh?