Proper etiquette for handler code that resets with character?

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?

Not really sure what you mean. However, I have used a lot of scripts and module scrips in startercharacter that needed to be reinitialized when the character respawns. I never ran into any problems.

Yeah, I started searching and found out I might just be creating a non-issue as ReplicatedStorage doesn’t really seem intended for “handler code” interacting with the character/PlayerGui anyway and should instead be placed in StarterPlayerScripts, StarterCharacterScripts or ReplicatedFirst clarified in this comment in a related post:

1 Like

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