generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: How do I convert this to a non-anonymous function?

Previous Thread :: Next Thread 
EgoInclined is not online. EgoInclined
Joined: 11 Sep 2015
Total Posts: 98
24 Mar 2016 10:05 PM
I'm trying to pass extra parameters to an event listener.


-- I'm trying to convert this to a non-anonymous function
imgButton.MouseButton1Down:connect(function(button)
button = imgButton
print(button.Name)
end)


But, if I try, then how do I pass the button parameter to the :connect() method?

for example:
function buttonClicked(button)
print(button.Name)
end

imgButton.MouseButton1Down:connect(buttonClicked) -- ** I don't know how to pass the imgButton as an argument to the function
Report Abuse
ExpensiveGaming is not online. ExpensiveGaming
Joined: 01 Apr 2013
Total Posts: 783
24 Mar 2016 10:25 PM
function buttonClicked(button, imgButton)
print(button.Name)
end


#code print("lol im batman")
Report Abuse
me2kool2talk2u is not online. me2kool2talk2u
Joined: 19 Feb 2007
Total Posts: 2914
24 Mar 2016 10:38 PM
@Expensive that code didn't do anything, it just added another parameter to the function. He wanted to know how to pass the parameter from the event he connected the function to.

To answer your question, I believe you would pass it as normal:

function buttonClicked(button)
print(button.Name)
end

imgButton.MouseButton1Down:connect(buttonClicked(imgButton))
Report Abuse
Casualist is not online. Casualist
Joined: 26 Jun 2014
Total Posts: 4443
24 Mar 2016 10:44 PM
@2kool
function buttonClicked(button)
print(button.Name)
end

imgButton.MouseButton1Down:connect(buttonClicked(imgButton))

effectively behaves like:

function buttonClicked(button)
print(button.Name)
end

buttonClicked(imgButton)
imgButton.MouseButton1Down:connect(nil)




@OP This is what you want:

function buttonClicked(button)
print(button.Name)
end

imgButton.MouseButton1Down:connect(function()buttonClicked(imgButton)end)
Report Abuse
me2kool2talk2u is not online. me2kool2talk2u
Joined: 19 Feb 2007
Total Posts: 2914
24 Mar 2016 10:52 PM
Thanks. I always use lambda functions when I connect events.
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
24 Mar 2016 11:28 PM
save lines of code and just put the function inside the connection

thing.MouseButton1Click:connect(function()
--all your stuff in here
end)

looks cleaner imo
Report Abuse
me2kool2talk2u is not online. me2kool2talk2u
Joined: 19 Feb 2007
Total Posts: 2914
24 Mar 2016 11:33 PM
@TimeTicks Anonymous functions are great, but sometimes you want to be able to call the function from other points in the script. Also his question was how not to use anonymous functions.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image