I have “5” models in which have the same name in Workplace along with a ClickDetector in each of them. However, I’m looking to have a script that looks at the ClickDetector of all five of those models and initiating a function if a player clicks either of the models.
Let’s say I have five models that are named, “Model” and two models named, “Daddy.” (Habit, sorry). I’ve tried multiple times to create a function in which looked at everything in workplace for the name, “Daddy” and then looked at the ClickDetector and waited for a click. If either of the models, received a click response (or if a player just clicked one of the models, in other words) then a frame would become visible to the player. (Not that part that I’m looking for, I already have that part handled.)
What I’m looking to accomplish
Assuming a pairs function of some kind in which looks at each of the models if they are named a certain name such as “SpecialModel” or “Daddy.” (Random name I use for script.Parent locals. It’s habit.)
Looking to find out a way to accomplish the above if it is indeed possible or a way that includes changing the value of objects if they exist in the models that have a certain name.
What I’ve looked into or already tried.
I’ve looked online and through a couple of forums where others who have been hit with a similar but not exact problem. They were looking to change the value of the brick if it had the name, “Model” or “Part.” or even change the transparency. (Really specific but that’s what a couple videos did.)
I’ve watched a couple videos and came to no conclusion on how to do what I’m looking for “if” it’s even possible.
If you are looking for the ClickDetectors specifically, then I would suggest looking into learning CollectionService, specifically the functions under it AddTag and GetTagged.
Using CollectionService, you can tag different objects with a string, and use GetTagged to get every object with that tag no matter where it is. Not only is this a neater way of doing things, but you no longer need to loop through a bunch of irrelevant objects to what you’re looking for.
It isn’t the only way, but it’s arguably the best way. An easy way to add tags is with a tag editor plugin, rather than using the command bar or doing it at run time.
Once the objects are tagged, you could just get all of them and loop through them doing this:
For this example, the click detectors are given the tag “Clicker”, though you should give your tags more descriptive names
local CollectionService = game:GetService("CollectionService")
local Clickers = CollectionService:GetTagged("Clicker") --This returns a table of the click detectors
for _, Clicker in pairs(Clickers) do
--Make the frame visible
A different way is that you simply do what you suggested in your question, by looping through the children, checking for names, then doing whatever you want inside that object.
for _, Model in pairs(workspace:GetChildren()) do
if Model.Name = "ButtonModel" then
local Part = Model.Part
local ClickDetector = Part.ClickDetector
Part.Transparency = 1 -- just for example
print(Model.StringValue.Value) -- print the value of an object named StringValue inside the model
There are also two (three!) useful functions available to improve this, too. Object:WaitForChild handles getting an object if it isn’t in the object yet, Object:FindFirstChild is the same as normal Object.ChildName except it doesn’t error if the child doesn’t exist, and it can support looking through descendants and not just direct children. A “spinoff” method is FindFirstChildOfClass which is the same except it looks for class types (e.g. ClickDetectors, Parts) instead of names.
Personally I consider this to be the best solution for a beginner as it makes sure you understand the basics before you start skipping them. Once you’ve got it in your bag of code tricks it’s fine to move on to better solutions like tags, though.
Did you tag the click detectors? If so, with what tag?
Also a tip for the future is that if you have an issue, I would make a new post instead of editing an existing one. I wasn’t aware you were having issues, and wouldn’t have known if EmeraldSlash didn’t respond to the thread.