Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Reuse a function passing arguments when event occurs?

Hello,

I have created a function called openDoor() to open a sliding door when a button is clicked.

Now I would like to use the same function to open all the doors in my game using their respective buttons.

I'm assuming that button and door should be set as parameters in openDoor() and arguments should be passed when the event MouseClick trigger the function.

Based on my assumptions the arguments should be set to the specific "door" and "button" in my game.workspace that the player wants to open.

Unfortunately it doesn't work... The function for some reason is executed before the event MouseClick happens.

Any idea how to do it ?

Thank you in advance.

Best

local door1 = game.workspace.model.door1

local button1 = game.workspace.model.button1

local door5 = game.workspace.model.door5

local button5 = game.workspace.model.button5

openDoor(door, button)

-- change the status/color of the button

-- move the door

end

button1.ClickDetector.MouseClick:connect (openDoor(door1, button1))

button5.ClickDetector.MouseClick:connect (openDoor(door5, button5))

3 comments

0

Or you can call the function once but in the function you open the doors.

0

If you are needing help with scripting, you could let me know your skype or discord by PM'ing your name on roblox.

0

Thanks FireDusk! Really appreciated!

2 answers

The Problem

When you write openDoor(door1, button1), you are telling the program to run the this function. Therefore when the program gets to that connection line, the MouseClick event gets connected to what ever openDoor returned. This is not what you want to happen.


Solution

A better way to do this is to store all your doors and buttons in tables, whether that be by searching for them in your model and appropriately inserting them in or explicitly writing their locations. When they are both in tables, we can simply loop over all the buttons and connect a MouseClick event to them:

local model = game.Workspace.model
local doors = {model.door1, model.door5}
local buttons = {model.button1, model.button5}

for index = 1, #buttons do
	local door = doors[index]
	local button = button[index]

	button.ClickDetector.MouseClick:connect(function()
		-- change the status/color of the button
		-- move the door
	end)
end

Alternatively, you could set up a dictionary that directly associates each door with a button (or vice-versa) and loop over each pair with a generic for loop:

local model = game.Workspace.model
local doors = {
	model.door1 = model.button1,
	model.door5 = model.button5
}

for door, button in pairs(doors) do
	button.ClickDetector.MouseClick:connect(function()
		-- change the status/color of the button
		-- move the door
	end)
end

Edit

I noticed too late that you already found a solution, but I will leave this here as an alternative solution (which personally I think is more readable)

2 comments

0

Thank you for the explanation BlackJPI! Very clear and useful. Your solution is definitely much more readable. Thxx

0

No problem!

Hi I have found the answer here!

https://scriptinghelpers.org/questions/6692/how-does-one-pass-a-connect-line-optional-parameters-with-a-named-function

"When you connect an event, it adds its own parameters based on the event. Attempting to add your own via the connection line will produce an error. A solution is to call your named function with an anonymous function."

In my example :

button1.ClickDetector.MouseClick:connect (function()openDoor(door1, button1)

end)

Thanks MrFlimsy!!

This discussion is preserved as it was. New answers, comments, and votes are closed.