function clickedButton()
-- Code
end)
Button.MouseButton1Click:Connect(clickedButton()
How could i do something like this:
function clickedButton(buttonName)
if buttonName == Button1 then
-- Code
end
end)
Button.MouseButton1Click:Connect(clickedButton(Button1)
I also want to send the Button Name, but Button.MouseButton1Click:Connect(clickedButton(Button1) will not work, it gives me a Error. How could i do it?
local function HookButton(Target, Callback)
local ButtonName = Target.Name
local HookFunction = function()
Callback(ButtonName)
end
return Target.MouseButton1Click:Connect(HookFunction)
end
What you could do is put a localscript inside the button and an IntValue instance.
Inside the localscript you do an event and you send the id of your button that you manually inserted
local ReplicatedStorage = game:WaitForChild('ReplicatedStorage')
local RemoteEventID = ReplicatedStorage:WaitForChild('GetItemIDRemoteEvent') -- your remote event
local ID = script.Parent.PartID.Value -- your button ID
button.MouseButton1Click:Connect(function()
RemoteEventID:FireServer(ID) -- Send the ID of the button to the server
end)
Based on that you know which button is which, so in your main script you can do this
RemoteEventID.OnServerEvent:Connect(function(plr, ID)
if buttonID == ID then
--Code
end
end)
local function HookButton(buttonName, Callback)
local ButtonName = buttonName.Name
local HookFunction = function()
Callback(ButtonName)
end
return buttonName.MouseButton1Click:Connect(HookFunction)
end
connection = HookButton(driverButton, buttonClicked)
connection = passengerButton.MouseButton1Click:Connect(buttonClicked(passengerButton))
connection = infoButton.MouseButton1Click:Connect(buttonClicked(infoButton))
I also dont understand how this works how does connection = HookButton(Button1, buttonClicked) fire when MouseButton1Click is inside function, i didnt know that works
It works by taking the Button you want to connect to, the function you want it to fire on click, and then recording the name of the button, it makes a wrapper function “HookFunction” that is called when the button is pressed. When HookFunction is called it calls “Callback” (buttonClicked function) and passes “ButtonName” which is the recorded name of your button.
So just put this somewhere between HookButton and “connection = HookButton(…)”
local function buttonClicked(NameOfButton)
print(("Button %s was clicked!"):format(NameOfButton)
-- Do code here. Statement above is example, you can remove it.
end