Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

MORE ON ANONYMOUS FUNCTIONS??

Asked by
LevelKap 114
9 years ago
Edited 7 years ago

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)


0
To elaborate on your question in your last script LevelKap, yes, you can call a function like that, but it'll only run the one time. (But its called inside the function so it wont run until the function equipStuff is called) magiccube3 115 — 9y
0
(I answered you) Perci1 4988 — 9y

1 answer

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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!

1
Did I write the second part correctly? I was just confused on how the Button1Down anon function was able to use the returned value "mouse" as the object in the connection line. Is that only because they're stacked? Because with normal functions you can't use the argument of another function as the connection line for another function nor Acces the argument from another script LevelKap 114 — 9y
0
Yes, it can only use the mouse parameter because they're stacked. This is because the second event is still *inside* the first event. This is a huge advantage for anonymous functions. Your code is right, except you're missing an end. It's kind of weird though, considering you're calling Click every time you equip. That wiki post is a bit outdated, though. Use the Activated event. Perci1 4988 — 9y
Ad

Answer this question