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:
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.