Detect click on an instance cloned and added to Workspace from a LocalScript

I have in instance in ReplicatedStorage > Boxes called Box1. Using a LocalScript in StarterPlayer > StarterPlayerScripts I clone it and make the workspace its parent.

That works fine. I can see it. The code runs without errors. A print I have on the last line can be seen in the Output window

local box = game:GetService('ReplicatedStorage').Boxes.Box1
box:Clone().Parent = workspace

local clickDetector = Instance.new("ClickDetector",box)
function onBoxClicked(player)
  print("onBoxClicked")
end

clickDetector.MouseClick:Connect(onBoxClicked)
print("end of boxes controller script")

…but I can’t detect clicks on it. What do I need to do?

I’m pretty sure It’s because you’re actually inserting a clickdetector on the Box still inside ReplicatedStorage, if you want to put it in the Cloned Box you should make it a variable like so:

local box = game:GetService('ReplicatedStorage').Boxes.Box1
local ClonedBox = box:Clone()
ClonedBox.Parent = workspace

local clickDetector = Instance.new("ClickDetector",ClonedBox)
function onBoxClicked(player)
  print("onBoxClicked")
end

clickDetector.MouseClick:Connect(onBoxClicked)
print("end of boxes controller script")
1 Like

Yep, that’s it. Thanks. I don’t know how I missed that but I think it’s time to call it a day!