!
I tried to place 1 single local script under each of the click detectors but that didnt work for some reason and i think its cuz u cant put local script in work space so i did it on playercharacterscripts
but how would i define all the clickdetectors since the vendingmachines have the same name just diffrent colors
function clicked(detector)
-- stuff here
end
for I, v in pairs(workspace.World.VendingMachines:GetDescendants()) do
if v:IsA("ClickDetector") then
v.MouseClicked:Connect(function()
clicked(v)
end)
end
end
This will make all the ClickDetectors’ MouseClicked event fire the same function, passing in the ClickDetector object that was clicked as a variable.
1 Like
i c i c
i got 1 more question
wats the diffrence between:
function testfunction()
print("do stuff")
end
ClickerDetector.MouseClick:Connect(testfunction())
and
ClickDetector.MouseClick:Connect(function()
print("do stuff")
end)
wrote dis on the forum page so if any errors yeah
The difference is that one is a function definition followed by an event connection to that function, which includes a name, and therefore allows it to be accessed by other calls. The other one defines the function to be run within the parameters of the event connection, which is more streamlined, but doesn’t allow it to be called anywhere else in the script.
So essentially in the code sample I provided above, you’re actually defining as many small functions as you have ClickDetectors, that just link back to the clicked()
function.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local VendingMachine = ReplicatedStorage.RemoteEvents.VendingMachine
for i,v in pairs(game.Workspace.World.VendingMachines:GetDescendants()) do
local drink = nil
if v:IsA("ClickDetector") then
drink = v.Parent.Parent.Name
v.MouseClick:Connect(function()
VendingMachine:FireServer(drink)
end)
end
end
tanks