hello so i have money, or better known as a part in workspace and i have this clickdector code that only works for one dollar, code below, but it does not work with the parts that are cloned
1 | game.Workspace:WaitForChild( "dollar" ).ClickDetector.MouseClick:Connect( function () |
2 | print ( "WOWWWWWWW" ) |
3 | end ) |
MAKE SURE TO PUT THE SCRIPT INSIDE THE PART.
1 | script.Parent.ClickDetector.MouseClick:Connect( function () |
2 | - - Event |
3 | end ) |
Now if you clone the part, the script should be cloned as well. The reason why your code didn't work is because you told the script to look for that object only and not any other objects like it. It should work now.
If you are looking for this block of code to work for every click detector in workspace called 'Dollar', then the best thing to do would be to use a for loop and search through the descendants in workspace.
Example
1 | for _, detectors in pairs (workspace:GetDescendants()) do |
2 | if detectors:IsA( "ClickDetector" ) and tostring (detectors) = = "Dollar" then |
3 | --your code |
4 |
5 | end |
6 | end |
A for loop will run the same code for every click detector that is named "Dollar"
Now we will attach an event to all of them
1 | for _, detectors in pairs (workspace:GetDescendants()) do |
2 | if detectors:IsA( "ClickDetector" ) and tostring (detectors) = = "Dollar" then |
3 |
4 | detectors.MouseClick:Connect( function (playerWhoClicked) |
5 | print ( "OwO UwU" ) |
6 | end ) |
7 |
8 | end |
9 | end |