Alright, so i've asked something similar to this, but i don't think i explained what i was asking very well, so here it goes:
I've decided to learn raycasting, so after reading up on it in the wiki, I decided to make a raycasting laser gun! Now, as with a lot of scripts on the wiki roblox uses anonymous functions in their examples. This is what they used to start the raycasting local script: Hierarchy: Players>LevelKap>Backpack>PaintballGun>LocalScript
local tool = script.Parent local user tool.Equipped:connect(function(mouse) mouse.Button1Down:connect(function() end) end)
Now, they "stack" these two function together, and i noticed that to write the clicked function(Button1Down), they use "mouse" as the object, which was never called **until ** it was returned in the argument for the equipped function. Do stacked anonymous function have the ability to call arguments from other functions with events? Also, how would i go about doing this without using anonymous function? Wouldn't I have to define mouse separately? Something like this:
Player = game.Players.LocalPlayer Mouse = Player:GetMouse() local tool = script.Parent local user function Click() print("stuff was clicked") Mouse.Button1 Down:connect(Click) function equipStuff() Click() -- can i even call click without it's event being triggered? end tool.Equipped:connect(equipStuff)
Yes, you can use your function in whatever way you please. This means that you can connect it to any number of events, or just call it normally. For example, the following code is valid:
function printHello() print("Hello!") end printHello() script.Parent.Touched:connect(printHello) workspace.ChildAdded:connect(printHello) game.Players.PlayerAdded:connect(printHello)
This code will print when the code runs, when script.Parent
is touched, when a child is added to workspace, and when a player joins the game. None of these interfere with the others.
I'm not sure what anonymous functions have to do with this, though. Comment with questions!