Talk:Anonymous Functions

From Legacy Roblox Wiki
Jump to navigationJump to search

"however this should be avoided in code that runs more than once." I'd never thought of that. So many of my scripts would, say, loop all the zombies in the game and connect events to them anonymously, whereas as you've pointed out there it'd've been much more efficient to point to a single function.

Trappingnoobs (Writer) Have I done something bad? Good? Tell me on my talk page 21:46, 26 December 2011 (UTC)
Yes, but normally you need an anonymous function to keep a closure on local variables:
 for _, zombie in ipairs(zombies) do
    zombie.Torso.Touched:connect(function(by)
        zombie.Head:remove() --You can still refer to zombie - not possible otherwise
    end)
end
14:55, 27 December 2011 (UTC)
There's a way around that, though, just take the zombies variable out of the local scope
Trappingnoobs (Writer) Have I done something bad? Good? Tell me on my talk page 22:07, 28 December 2011 (UTC)