-- Timed ability state sample -- Redact Studios / trulyagents -- -- This sample demonstrates tokenized cleanup for a temporary visibility skill. -- A stale timer can never cancel a newer activation, and every visual property -- is restored from a snapshot instead of being hard-coded. local Players = game:GetService("Players") local MAX_DURATION = 5.5 local activeTokens = {} local function captureCharacterState(character) local state = { parts = {}, decals = {}, nameDisplay = nil, } for _, instance in character:GetDescendants() do if instance:IsA("BasePart") then state.parts[instance] = instance.Transparency elseif instance:IsA("Decal") or instance:IsA("Texture") then state.decals[instance] = instance.Transparency end end local humanoid = character:FindFirstChildOfClass("Humanoid") if humanoid then state.nameDisplay = humanoid.DisplayDistanceType end return state end local function applyHiddenState(character) for _, instance in character:GetDescendants() do if instance:IsA("BasePart") or instance:IsA("Decal") or instance:IsA("Texture") then instance.Transparency = 1 end end local humanoid = character:FindFirstChildOfClass("Humanoid") if humanoid then humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None end end local function restoreCharacterState(character, state) for part, transparency in state.parts do if part.Parent and part:IsDescendantOf(character) then part.Transparency = transparency end end for visual, transparency in state.decals do if visual.Parent and visual:IsDescendantOf(character) then visual.Transparency = transparency end end local humanoid = character:FindFirstChildOfClass("Humanoid") if humanoid and state.nameDisplay then humanoid.DisplayDistanceType = state.nameDisplay end end local function activateTemporaryVisibility(player) local character = player.Character if not character then return false end local token = {} activeTokens[player] = token local snapshot = captureCharacterState(character) applyHiddenState(character) task.delay(MAX_DURATION, function() if activeTokens[player] ~= token then return end activeTokens[player] = nil if player.Character == character then restoreCharacterState(character, snapshot) end end) return true end Players.PlayerRemoving:Connect(function(player) activeTokens[player] = nil end) return activateTemporaryVisibility