I’ve looked up a lot of things on the topic, but none of them appear to work. I don’t get it, and I’m still a newbie at lua.
So I have 3 parts here named clicker and I want to make it work with only one function.
I really need help
I’ve looked up a lot of things on the topic, but none of them appear to work. I don’t get it, and I’m still a newbie at lua.
So I have 3 parts here named clicker and I want to make it work with only one function.
I really need help
Well the most basic method would be as follows:
local clickDetector1 = workspace.Part1.ClickDetector
local clickDetector2 = workspace.Part1.ClickDetector
local clickDetector3 = workspace.Part1.ClickDetector
function onMouseClick()
print("You clicked me!")
end
clickDetector1.MouseClick:connect(onMouseClick)
clickDetector2.MouseClick:connect(onMouseClick)
clickDetector3.MouseClick:connect(onMouseClick)
A slightly cleaner and better approach would be to simply loop through the elements. Lets say you have all the parts within a model in the workspace, you could do as follows:
local model = game.Workspace.Model
function onMouseClick()
print("You clicked me!")
end
for i,v in pairs(model:GetChildren()) do
local clickDetector = v:FindFirstChild("ClickDetector")
if clickDetector then
clickDetector.MouseClick:connect(onMouseClick)
end
end)
With this approach you could keep adding the parts into the model and their click cetectors would automatically be linked to the onMouseClick function, without having to change any code.
Can I put it in a folder with multiple parts that has a clickdetector?
A folder works fine, just make sure to change the variable path accordingly.
Thank you so much! Works perfectly Fine!
By the way, I added the parts into the model but the script stopped working
The clickdetectors are insidee the model with the part.
You can try using GetDescendants instead of GetChildren, and check if “v” is a BasePart.
model:GetDescendants()
-- Inside the for loop...
if v:IsA("BasePart") then
local clickDetector = v:FindFirstChild("ClickDetector")
if clickDetector then
clickDetector.MouseClick:connect(onMouseClick)
end
end
for i,v in pairs(model:GetChildren()) do
if v:IsA(“BasePart”) then
local clickDetector = v:FindFirstChild(“ClickDetector”)
if clickDetector then
clickDetector.MouseClick:connect(onMouseClick)
end
end
local clickDetector = v:FindFirstChild(“ClickDetector”)
if clickDetector then
clickDetector.MouseClick:connect(onMouseClick)
end
end)
like this?
Change model:GetChildren() to model:GetDescendants()
for i,v in pairs(model:GetDescendants()) do
if v:IsA(“BasePart”) then
local clickDetector = v:FindFirstChild(“ClickDetector”)
if clickDetector then
clickDetector.MouseClick:connect(onMouseClick)
end
end
local clickDetector = v:FindFirstChild(“ClickDetector”)
if clickDetector then
clickDetector.MouseClick:connect(onMouseClick)
end
end)
Now, try testing your code and check if there are still problems.
If the path to the click detector is always the same no matter the model then I would just update the path in the loop
Ex.
local clickDetector = v.Part:FindFirstChild("ClickDetector")
Otherwise, as @Y_VRN mentioned, you could just change
:GetChildren()
to
:GetDescendants()
The red is because those arent actual quotes. Maybe you copied from the above and it changed the format?
Sorry im very dumb when it comes to scripting
Have you changed the names of the ClickDetectors? Try using FindFirstChildOfClass(ClassName) instead.
Can you try this and see if Step1 and Step2 both print
local model = game.Workspace.Folder
function onMouseClick()
print("You clicked me!")
end
for i,v in pairs(Folder:GetDescendants()) do
if v:IsA("ClickDetector") then
print("Step1")
v.MouseClick:connect(onMouseClick)
end
end)