Skip to content

.tagInstance immediate #8

@CluelessD3v

Description

@CluelessD3v

Summary

Add a skip parameter to ConnectUtil.tagInstance so it fires the appropriate
callback immediately on call added if the instance is already tagged,
removed if it is not matching the behaviour attributeChanged and
valueChanged already provide.

Motivation

tagInstance reacts to future tag changes but ignores current state. To handle
the initial case you have to write a manual check alongside the call:

IRL example of my use case:

-- manual initial state check
if player.Character:HasTag(Registry.tags.participant) then
    onPlayerParticipatingInMatch()
else
    onPlayerInLobby()
end

-- then the reactive listener
ConnectUtil.tagInstance(player.Character, Registry.tags.participant,
    function(_instance) onPlayerParticipatingInMatch() end,
    function(_instance) onPlayerInLobby() end
)

The check and the listener are saying the same thing twice. Every other
ConnectUtil function that reads current state fires immediately by default and
accepts skip = true to opt out. tagInstance should be consistent.

Design

Add skip: boolean? as a fourth parameter. When skip is absent or false,
tagInstance fires one callback immediately after wiring the signals:

  • instance has the tag → calls added(instance)
  • instance does not have the tag → calls removed(instance)

With skip = true neither callback fires on call; behaviour is unchanged from
today.

function Module.tagInstance(
    instance:  Instance,
    tag:       string,
    added:     (Instance) -> ()?,
    removed:   (Instance) -> ()?,
    skip:      boolean?
)

The call site above collapses to:

ConnectUtil.tagInstance(player.Character, Registry.tags.participant,
    function(_instance) onPlayerParticipatingInMatch() end,
    function(_instance) onPlayerInLobby() end
)

Drawbacks

  • Callers who previously paired a manual HasTag check with tagInstance will
    fire the callback twice on startup unless they add skip = true. This is a
    silent behaviour change for existing call sites.

Alternatives

Keep the manual check

Works, but duplicates the branch logic and breaks the consistency every other
ConnectUtil function already provides.

Metadata

Metadata

Labels

bugSomething isn't workingenhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions