Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

how to apply this clickdetector to work for every part that is a dollar?

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

game.Workspace:WaitForChild("dollar").ClickDetector.MouseClick:Connect(function()
	print("WOWWWWWWW")
end)

2 answers

MAKE SURE TO PUT THE SCRIPT INSIDE THE PART.

script.Parent.ClickDetector.MouseClick:Connect(function()
	- - Event
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

for _, detectors in pairs(workspace:GetDescendants()) do 
	if detectors:IsA("ClickDetector") and tostring(detectors) == "Dollar" then
		--your code
	
	end
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

for _, detectors in pairs(workspace:GetDescendants()) do 
	if detectors:IsA("ClickDetector") and tostring(detectors) == "Dollar" then
		
		detectors.MouseClick:Connect(function(playerWhoClicked)
			print("OwO UwU")
		end)
	
	end
end

2 comments

0

im sorry i get how this works but im still having issues getting the click detector to reference the dollar >.< ive tried adding detectors.ClickDetector.MouseClick:Connect(function(playerWhoClicked) but that didnt work also :/

0

Did you use my exact code? If so, then change Dollar to dollar

This discussion is preserved as it was. New answers, comments, and votes are closed.