Skip to content

Fix exit callbacks not firing on Observer unsubscribe#16

Merged
LDGerrits merged 1 commit intoLDGerrits:mainfrom
C6H15:fix/observer-unsubscribe-exit
Apr 9, 2026
Merged

Fix exit callbacks not firing on Observer unsubscribe#16
LDGerrits merged 1 commit intoLDGerrits:mainfrom
C6H15:fix/observer-unsubscribe-exit

Conversation

@C6H15
Copy link
Copy Markdown
Contributor

@C6H15 C6H15 commented Apr 9, 2026

When calling Observer:unsubscribe(Group), exit callbacks for entities inside zones aren't called. PR replaces stale cache lookup with a proper check, like how it's already done with group removals.

Test (Client)
--!strict
local Players = game:GetService("Players")

local Player = Players.LocalPlayer

local QuickZone = require(workspace.QuickZone)

local ZonePart = Instance.new("Part")
ZonePart.Anchored = true
ZonePart.CastShadow = false
ZonePart.CanCollide = false
ZonePart.Name = "Test"
ZonePart.Position = vector.zero
ZonePart.Size = vector.one * 20
ZonePart.Material = Enum.Material.ForceField
ZonePart.Color = Color3.fromRGB(0, 0, 255)
ZonePart.Parent = workspace

local Zone = QuickZone.Zone.fromPart(ZonePart)
local Group = QuickZone.Group.localPlayer()
local Observer = QuickZone.Observer.new({
   ["groups"] = {Group},
   ["zones"] = {Zone},
})

Observer:onLocalPlayerEnter(function(Zone)
   local Character = Player.Character
   if Character == nil then return end
   if Character:FindFirstChildOfClass("Highlight") == nil then
   	local Highlight = Instance.new("Highlight")
   	Highlight.FillTransparency = 0.25
   	Highlight.FillColor = Color3.fromRGB(255, 0, 0)
   	Highlight.Parent = Character
   end
end)

Observer:onLocalPlayerExit(function(Zone)
   local Character = Player.Character
   if Character == nil then return end
   local Highlight = Character:FindFirstChildOfClass("Highlight")
   if Highlight ~= nil then Highlight:Destroy() end
end)

task.delay(5, function()
   local Character = Player.Character
   if Character == nil then warn("Character is missing.") return end
   Observer:unsubscribe(Group)
   if Character:FindFirstChildOfClass("Highlight") == nil then
   	print("Observer has unsubscribed, the highlight has been removed.")
   else
   	print("Observer has unsubscribed, but the highlight still exists.")
   end
end)
Test (Server)
--!strict
local QuickZone = require(workspace.QuickZone)

local ZonePart = Instance.new("Part")
ZonePart.Anchored = true
ZonePart.CastShadow = false
ZonePart.CanCollide = false
ZonePart.Name = "Test"
ZonePart.Position = vector.zero
ZonePart.Size = vector.one * 20
ZonePart.Material = Enum.Material.ForceField
ZonePart.Color = Color3.fromRGB(0, 0, 255)
ZonePart.Parent = workspace

local Zone = QuickZone.Zone.fromPart(ZonePart)
local Group = QuickZone.Group.players()
local Observer = QuickZone.Observer.new({
   ["groups"] = {Group},
   ["zones"] = {Zone},
})

Observer:onPlayerEnter(function(Player, Zone)
   local Character = Player.Character
   if Character == nil then return end
   if Character:FindFirstChildOfClass("Highlight") == nil then
   	local Highlight = Instance.new("Highlight")
   	Highlight.FillTransparency = 0.25
   	Highlight.FillColor = Color3.fromRGB(255, 0, 0)
   	Highlight.Parent = Character
   end
end)

Observer:onPlayerExit(function(Player, Zone)
   local Character = Player.Character
   if Character == nil then return end
   local Highlight = Character:FindFirstChildOfClass("Highlight")
   if Highlight ~= nil then Highlight:Destroy() end
end)

task.delay(5, function()
   for _, Player in game:GetService("Players"):GetPlayers() do
   	local Character = Player.Character
   	if Character == nil then warn("Character is missing.") return end
   	Observer:unsubscribe(Group)
   	if Character:FindFirstChildOfClass("Highlight") == nil then
   		print("Observer has unsubscribed, the highlight has been removed.")
   	else
   		print("Observer has unsubscribed, but the highlight still exists.")
   	end
   end
end)

Replaces stale cache lookup with a proper check, like how it's already done with group removals.
@LDGerrits LDGerrits merged commit e60e95e into LDGerrits:main Apr 9, 2026
@C6H15 C6H15 deleted the fix/observer-unsubscribe-exit branch April 9, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants