How to pass an argument as a function?

How do I pass an argument as a function?

For example:

-- Module script ------
function UIClickDetection.OnButtonClick(button, functionToDo)
	button.MouseButton1Click:Connect(functionToDo)
end

-- Server script ------
local function DoSomething(button)
	print(button.Name)
end
UIClickDetection.OnButtonClick(someButton, DoSomething)

Will this work?

1 Like

You have to do it like this:

button.MouseButton1Click:Connect(function()
    functionToDo(button)
end)

The button doesn’t automatically get passed

1 Like

Oh yeah lol, but do you know if it will work now?

Well, you’d have to test it. Passing an argument in like that does work and it’s the only way. As long as:

and

work, then everything will.

Ok I will try
char limit sigh**

I am a bit confused on what I should do, when I am calling the module’s function for the second parameter how do I pass the function?

UIClickDetectionModule.OnButtonClick(quickBuildMainGuiClonePositiveButton, functionToDo())
-- Do I need to pass the parameter where I am calling the module function?

Pass it without the parameters as you would any function to a connection event

I am getting an error saying:

 user_QuickBuild.rbxmx.QuickBuild.Main.UI.UIClickDetection:16: attempt to call a nil value  -  Edit - UIClickDetection:16

Just to be clear I am creating a plugin if it has anything to do with it
I am getting an error at here:

button.MouseButton1Click:Connect(function()
    functionToDo(button)
end)
1 Like

@domboss37
do you know what the problem is?

1 Like

Can you show the full code for that. Where are you Instancing functionToDo

1 Like

Here is my main plugin code, sorry if it’s messy

-- QuickBuild
local QuickBuild = script.Parent

-- Modules Folders
local ObjectModules = script.Object
local ObjectPositioningModules = ObjectModules.Positioning

local UIModules = script.UI

-- Modules

-- ObjectPositioning
local PositionObjectModule = require(ObjectPositioningModules.PositionObject)

-- UI
local UIClickDetectionModule = require(UIModules.UIClickDetection)
local MainPluginUiModule = require(UIModules.MainPluginUI)

-- Services
local ChangeHistoryService = game:GetService("ChangeHistoryService")
local Selection = game:GetService("Selection")

-- Create a new toolbar section titled "Custom Script Tools"
local toolbar = plugin:CreateToolbar("Quick Build")

-- Add a toolbar button named "Create Empty Script"
local quickBuildButton = toolbar:CreateButton("Open QuickBuild UI", "Open the QuickBuild UI", "rbxassetid://4458901886")

-- Make button clickable even if 3D viewport is hidden
quickBuildButton.ClickableWhenViewportHidden = false

-- Tables
local newSelection = {}

-- Debounces
local Opened = false

-- Create new "DockWidgetPluginGuiInfo" object
local quickBuildWidgetInfo = DockWidgetPluginGuiInfo.new(
	Enum.InitialDockState.Left,
	false,   -- Widget will be initially enabled
	false,  -- Don't override the previous enabled state
	200,    -- Default width of the floating window
	300,    -- Default height of the floating window
	150,    -- Minimum width of the floating window (optional)
	150     -- Minimum height of the floating window (optional)
)

MainPluginUiModule:createMainPluginUI(quickBuildWidgetInfo, "Quick Build")

local QuickBuildWidget = MainPluginUiModule:GetMainPluginUI()

local QuickBuildMainGui = QuickBuild.QuickBuildMainGui
QuickBuildMainGui.Parent = QuickBuildWidget

local quickBuildMainGuiClonePositiveButton = QuickBuildMainGui.ClonePositive
local quickBuildMainGuiCloneNegativeButton = QuickBuildMainGui.CloneNegative

-- Open/Close GUI
quickBuildButton.Click:Connect(function()
	if Opened then
		QuickBuildWidget.Enabled = false
		Opened = false
	else
		QuickBuildWidget.Enabled = true
		Opened = true
	end
end)

local function onPositionButtonClicked(button)
	local selectedObject = Selection:Get()
	--local parent = game:GetService("ServerScriptService")
	if #selectedObject == 1 then
		if selectedObject and #selectedObject == 1 and selectedObject[1] ~= nil and selectedObject[1]:IsA("Part") then
			local objectToClone = selectedObject[1]:Clone()
			
			PositionObjectModule:PositionObject(objectToClone, "Positive")
			
			-- Set selection to object cloned for efficiency
			table.insert(newSelection, objectToClone)
			
			Selection:Set(newSelection)
			
			-- Remove from table so multiple objects don't get selected
			table.remove(newSelection, 1)
			
			ChangeHistoryService:SetWaypoint("Positioned the selected object")
		end	
		
	elseif #selectedObject > 1 then
		warn("Cannot select more than one object")
		
	elseif #selectedObject < 1 then
		warn("Select at least one object")	
	end	
end

UIClickDetectionModule.OnButtonClick(quickBuildMainGuiClonePositiveButton, onPositionButtonClicked())	

Here is my module script code:

-- QuickBuild
local QuickBuild = script.Parent.Parent.Parent

-- Modules
local MainPluginUiModule = require(QuickBuild.Main.UI.MainPluginUI)
local PositionObjectModule = require(QuickBuild.Main.Object.Positioning.PositionObject)

-- Main Plugin UI
local QuickBuildWidget = MainPluginUiModule:GetMainPluginUI()

-- Module code
local UIClickDetection = {}

function UIClickDetection.OnButtonClick(button, functionToDo)
	button.MouseButton1Click:Connect(function()
		functionToDo(button)
	end)
end

-- End of Modules
------------------

-- Return module calls
return UIClickDetection

you gotta remove those from the line:

Oh wow that works thanks!!!

1 Like