I am trying to make a script when you touch a part it will get cloned, and by touching the cloned parts it will also get cloned.
My current issue is how do I go detecting cloned parts? I’ve spent a few hours in searching various solutions including searching topics here but I still can’t find a solution, and so I have decided to write this topic.
I’m using Touched Event for my script, if there’s a better way to do this,
please do let me know!
This is my code:
local ball = game.Workspace.ball
local ball_value = 1
debounce = false
ball.Touched:Connect(function(touched)
if debounce == false and touched.Parent:FindFirstChild("Humanoid") then
debounce = true
local newball = ball:Clone()
ball_value = ball_value + 1
newball.Name = "ball"
wait(1)
print("ball count", ball_value)
newball.Parent = game.Workspace
wait(3)
end
debounce = false
end)
For something like this, I would use CollectionService. With CollectionService, you can add Tags to your cloned parts. CollectionService also has a nifty function called GetInstanceAddedSignal which can run a function when a new part gets tagged. In your case you can use GetInstanceAddedSignal to create a touched event for which ever part a tag was added to. An example script is below!
local CollectionService = game:GetService("CollectionService");
local alreadyTaggedParts = CollectionService:GetTagged("CloningPart");
local onTouchedFunction = function(partTouched)
-- Check if humanoid and then clone the part here!
end
for _, part in pairs(alreadyTaggedParts) do
part.Touched:Connect(onTouchedFunction)
end
CollectionService:GetInstanceAddedSignal("CloningPart"):Connect(function(newObjectTagged)
newObjectTagged.Touched:Connect(onTouchedFunction)
end)
EDIT: I suggest utilizing a tag-editor plugin such as this one. It makes tagging parts in studio a whole lot easier!
local ball = game.Workspace.ball
local ball_value = 1
debounce = false
local function OnTouched(touched, ball)
if debounce == false and touched.Parent:FindFirstChild("Humanoid") then
debounce = true
local newball = ball:Clone()
ball_value = ball_value + 1
newball.Name = "ball"
newball.Touched:Connect(function(touched)
OnTouched(touched, newball)
end) -- connect the Touched event to the cloned part
wait(1)
print("ball count", ball_value)
newball.Parent = game.Workspace
wait(3)
end
debounce = false
end
ball.Touched:Connect(function(touched)
OnTouched(touched, ball)
end)
I’ve tested it, unfortunately it does not work as intended.
When player touches the part it does clone, but when the cloned part touches the part, it also clones. Thanks for telling me about Collection Service though, it is a great knowledge to have around!