Instantiating ProximityPrompt in tagged instances parented to nil causing Triggered, TriggerEnded to fire twice

I have code which tags a Model parented to nil before parenting the Model. It listens for the tag added with CollectionService:GetInstanceAddedSignal(), waits for the Model to be a descendant of the workspace, then creates a ProximityPrompt parented to the Model. When the Model’s parent is nil when it is tagged, the instantiated ProximityPrompt will fire its Triggered and TriggerEnded signals twice upon player input. This issue does not occur if the Model is parented before being tagged using CollectionService:AddTag(), but occurs even when the ProximityPrompt is created after waiting for the Model to be parented.

Reproduction code:

local CollectionService = game:GetService("CollectionService")
local tagName = "TestObject"

local greenModel = Instance.new("Model")
greenModel.Name = "Green Model"
local part = Instance.new("Part")
part.BrickColor = BrickColor.Green()
part.CFrame = CFrame.new(5, 5, -5)
part.Parent =  greenModel
greenModel.PrimaryPart = part

local redModel = Instance.new("Model")
redModel.Name = "Red Model"
local part = Instance.new("Part")
part.BrickColor = BrickColor.Red()
part.CFrame = CFrame.new(-5, 5, -5)
part.Parent =  redModel
redModel.PrimaryPart = part

-- the code in question
-- only seems to occur when the prompt is created within this listener
CollectionService:GetInstanceAddedSignal(tagName):Connect(function(obj)
	print(`New {obj.Name}`)
	-- Happens when proximity prompt is added to the instance via CollectionService while object is parented to nil
	if obj.Parent == nil then
		obj.AncestryChanged:Wait()
	end
	-- Issue still occurs; the Instance was tagged when parented to nil
	local prompt = Instance.new("ProximityPrompt")
	prompt.Triggered:Connect(function()
		print(`{obj.Name} Triggered`)
	end)
	prompt.TriggerEnded:Connect(function()
		print(`{obj.Name} Trigger Ended`)
	end)
	prompt.Parent = obj
end)

task.wait(1)

-- the prompt will only fire once when interacted with
greenModel.Parent = workspace
CollectionService:AddTag(greenModel, tagName)

-- this results in the prompt firing twice when interacted with
CollectionService:AddTag(redModel, tagName)
redModel.Parent = workspace

Output:
image

Reproduction file:
ProximityPromptCollectionServiceBug.rbxl (45.5 KB)

Expected behavior

ProximityPrompt should only fire events once, regardless of when it is created or when its parent is tagged.

6 Likes

Thanks for the report! We’ll follow up when we have an update for you.

This should be fixed now. Thanks!

1 Like

This issue, or a related one, seems to have started happening again for me. The light switches in my game suddenly stopped working at some point in the last week(s), with no changes to the code, and I have found that it’s because the Triggered event is firing twice, toggling them on then off rapidly.

1 Like

I’ve currently experienced the same issue, the current loophole I’m using for this was waiting the instance to be in the workspace.

Heres and example code;

local CollectionService = game:GetService("CollectionService")

CollectionService:GetInstanceAddedSignal(tagName):Connect(function(obj)

	if not obj:IsAncestorOf(workspace) then
		obj.AncestryChanged:Wait()
	end
	
	local prompt = Instance.new("ProximityPrompt")
	prompt.Triggered:Connect(function()
		print(obj.Name .. " Triggered")
	end)
	
end

Its basically OP (@Eqicness) code, but changes obj.Parent == nil to not obj:IsAncestorOf(workspace) in the if statement.

1 Like

This is happening again. Verified that if you add an instance, it fires twice.

Created a repo. Seems to primarily happen if the SignalBehavior property in workspace is set to Default and creating a ProximityPrompt for a newly added instance. Works fine in Deferred.

repo_firing_twice.rbxl (53.5 KB)

1 Like