Answered by
8 years ago Edited 8 years ago
Unfortunately, returning values from an event callback is possible, but useless.
This is because your callback function is being called by Roblox, who doesn't expect, or simply doesn't care whether you return values or not. It will just discard them.
Upvalues
Thankfully there is a way around this. What you can do instead is use "upvalues". That sounds like a whole new concept, but the truth is you've probably already used them before without knowing what they were called. Upvalues are variables that are declared prior to your function, in an upper scope. For example:
01 | local tool = script.Parent |
06 | tool.Equipped:connect( function (mouse) |
07 | local user = tool.Parent |
09 | mouse.Button 1 Down:connect( function () |
12 | selection = mouse.Target |
20 | game:GetService( "UserInputService" ).InputBegan:connect( function (input, processed) |
24 | if input.KeyCode = = Enum.KeyCode.Delete or |
25 | input.KeyCode = = Enum.KeyCode.KeypadPeriod then |
27 | if selection and not selection.Locked then |
Conclusion
By setting and getting upvalues from within your functions, you can send information around without having to rely on returning values.
Anyway, I hope this helped. If it did then please be sure to accept, and optionally upvote, my answer. It helps both of us. ;) If you need anything else, you need only ask.