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 / why does this code work?

Previous Thread :: Next Thread 
MushySuperAbby is not online. MushySuperAbby
Joined: 17 Sep 2016
Total Posts: 19
02 Sep 2017 11:56 PM
Hello everyone,

Just trying to help my son create his own Roblox server world :)

Can someone help explain to us how this chunk of code works?

1. local trigger = game.Workspace.Shop.Front.Trigger
2. local door = script.Parent
3. local doorKnob = script.Parent.DoorKnob
4. local In############# game.Workspace.Shop.Front.InsideTrigger
5. local insideDoorKnob = script.Parent.InsideDoorKnob
6.
7. trigger.Touched:connect(
8. function(player)
9. if player.Parent:FindFirstChild("Humanoid") then
10. openFromOutside()
11. end
12. end)

"Trigger" with a capital T refers to a graphic / asset in his game. Its a floor mat like object when you step on it, it opens a door for 3 seconds. This code works but we're not sure why.

"trigger" with lowercase t seems to be an object. Like all objects, it has properties and functions / behaviors. So "trigger.Touched:connect() appears to be function call of some sort where the parameter / argument given is between lines 8-11.

I've never seen multiple lines of code INSIDE of what appears to be a function call? So is line #7 a function call? And what is line #8 doing?

Thanks in Advance ...
Dad.
Report Abuse
iiNemo is online. iiNemo
Joined: 22 Jul 2013
Total Posts: 2380
02 Sep 2017 11:59 PM
I'm struggling understanding what you just said
Report Abuse
Thane_1 is not online. Thane_1
Joined: 08 Apr 2009
Total Posts: 827
03 Sep 2017 12:01 AM
Basically it calls the function, but instead of using a predetermined function, it creates a function inside of it
Report Abuse
KEVEKEV77 is not online. KEVEKEV77
Joined: 12 Mar 2009
Total Posts: 6961
03 Sep 2017 12:02 AM
Hey Mushy,



the trigger with a local case t is called a variable. You can name it whatever you want, but whoever made that named it trigger so they knew it was assosciated with the object.

you could say local PukeFace = game.Workspace.Shop.Front.Trigger

and that would do you fine as well.

Then, it touches if someone touches this part names trigger, and if they do, it runs a function, which you did not post, openFromOutside.
Report Abuse
Thane_1 is not online. Thane_1
Joined: 08 Apr 2009
Total Posts: 827
03 Sep 2017 12:03 AM
Kind of like this example, where instead of doing the math beforehand, it does it in the parenthesis.

-----------
--Example1:

local A = 10 * 5
print(A)

------------
--Example 2:

print(10 * 5)







Report Abuse
iiNemo is online. iiNemo
Joined: 22 Jul 2013
Total Posts: 2380
03 Sep 2017 12:03 AM
game:GetService('Workspace').Shop.Front.Trigger.Touched:Connect(function(Hit) -- Detect If Part Is Being Touched
if Hit.Parent:FindFirstChildOfClass('Humanoid') then -- Checking To See If This Object That Touched It Has A Humanoid
openFromOutside() -- Call Some Other Function Thing That U Didn't Show Us
end
end)
Report Abuse
Duusskk is not online. Duusskk
Joined: 27 Aug 2013
Total Posts: 651
03 Sep 2017 12:06 AM
Yes, @Thane_1's example is quite the same thing. Functions can be put directly inside parameters just as math, strings, etc. can.

On line 7, there's a few things going on.

1. Touched is an event property of a part.
2. Events have a :connect() method that will let you execute a function whenever that event gets fired.
3. Y## ###t####s####unction into the :connect() method call. This is the function that will run, otherwise known as a "callback".

There's tw###a####ou can do this:

1. By defining a function normally.

function onTouched(player)
if player.Parent:FindFirstChild("Humanoid") then
openFromOutside()
end
end

trigger.Touched:connect(onTouched)

2. Or, by defining the function on the spot. This is called an anonymous function and is best suited for cases like this where you don't need to use this function anywhere else.

trigger.Touched:connect(function (player)
if player.Parent:FindFirstChild("Humanoid") then
openFromOutside()
end
end)

Report Abuse
amanda is not online. amanda
Joined: 21 Nov 2006
Total Posts: 5925
03 Sep 2017 12:12 AM
trigger.Touched:connect(function(player)
if player.Parent:FindFirstChild("Humanoid") then
openFromOutside()
end
end)

--

Touched is what we call a script signal or an event for the trigger object.

In the 3D world, whenever another object collides with trigger, it calls the function, passing the object that collided to the player parameter.

In the next line the parent of the object that hit is indexed:

player.Parent

--

and then the FindFirstChild method is used to get the first child of that parent named Humanoid, returning nil if one does not exist.

If the method returns the Humanoid object, then the condition passes and then function call for openFromOutside runs, otherwise it doesn't.

--

Whitespace doesn't matter, in the original script it could be considered bad practice only because it decreases readability to the common coder, however it will execute without error regardless.
Report Abuse
amanda is not online. amanda
Joined: 21 Nov 2006
Total Posts: 5925
03 Sep 2017 12:15 AM
refer to Duusskk's post for anonymous functions
Report Abuse
MushySuperAbby is not online. MushySuperAbby
Joined: 17 Sep 2016
Total Posts: 19
03 Sep 2017 12:21 AM
@Thane_1

So you're saying lines 8-11 is an actual function definition? If that is the case, where is the function's name?
Report Abuse
amanda is not online. amanda
Joined: 21 Nov 2006
Total Posts: 5925
03 Sep 2017 12:22 AM
it is an anonymous function

it doesn't have a name and cannot be called later

the purpose of it, is it is connected to an event, so it is triggered by that event throughout the game without additional code
Report Abuse
Thane_1 is not online. Thane_1
Joined: 08 Apr 2009
Total Posts: 827
03 Sep 2017 12:23 AM
The function doesn't need a name, it's a value like any other.

Like in my example, "print(A = 10 * 5)" is the exact same as print(10 * 5) except it has a pre-determined name.
Report Abuse
Thane_1 is not online. Thane_1
Joined: 08 Apr 2009
Total Posts: 827
03 Sep 2017 12:24 AM
Oops, "print(A = 10 * 5)" isn't an actual thing lol

I meant:

A=10*5
print(A)
Report Abuse
MushySuperAbby is not online. MushySuperAbby
Joined: 17 Sep 2016
Total Posts: 19
03 Sep 2017 12:28 AM
@KEVEKEK

On line #1, it looked like trigger (lower case "t") appears to be invoking a function call in line #7. I guess I'm used to calling anything that can store a property and values, and/or invoke function calls its an object or instance instead of a variable.

Thank for your response!
Report Abuse
MushySuperAbby is not online. MushySuperAbby
Joined: 17 Sep 2016
Total Posts: 19
03 Sep 2017 12:33 AM
@amanda

An anonymous event, never heard of it. I will look it up. That explains why it doesn't have a name. I still don't understand why I would use an anonymous function, why not just code it without the function since I can't use an anonymous function anyways?

I will research this, thanks!
Report Abuse
Thane_1 is not online. Thane_1
Joined: 08 Apr 2009
Total Posts: 827
03 Sep 2017 12:40 AM
Because when you use ":connect" it must have a function inside of it's brackets. This is the simplest way to create the function.
Report Abuse
Thane_1 is not online. Thane_1
Joined: 08 Apr 2009
Total Posts: 827
03 Sep 2017 12:45 AM
--You can create a function # ##### f = function() end --Or: function f() end --As you can see, f is a variable that is a function. Exactly like f = 5. When you use an event. (In your case trigger.Touched:connect) the connection needs a function to call. You can use ### ## the brackets, or you can create the function in the brackets. Creating it in the brackets removed the need for a name.
Report Abuse
MushySuperAbby is not online. MushySuperAbby
Joined: 17 Sep 2016
Total Posts: 19
03 Sep 2017 12:59 AM
@Thane

Thanks again,

It is just an odd concept for me that you could include lines of code within a function call. But I looked up the docs for "Touched" and sure enough...
http://wiki.roblox.com/index.php?title=API:Class/Humanoid/Touched

ALso found some interesting docs about how Lua functions work. This is quite different than what I'm used to in C/C##############e languages. Hard for me to wrap this old head around these ideas haha

Note the bit on "anonymous" functions.

From http##################################ore about Functions

Functions in Lua are first-class values with proper lexical scoping.

What does it mean for functions to be "first-class values"? It means that, in Lua, a function is a value with the same rights as conventional values like numbers and strings. Functions can be stored in variables (both global and local) and in tables, can be passed as arguments, and can be returned by other functions.

What does it mean for functions to have "lexical scoping"? It means that functions can access variables of its enclosing functions. (It also means that Lua contains the lambda calculus properly.) As we will see in this chapter, this apparently innocuous property brings great power to the language, because it allows us to apply in Lua many powerful programming techniques from the functional-language world. Even if you have no interest at all in functional programming, it is worth learning a little about how to explore those techniques, because they can make your programs smaller and simpler.

A somewhat difficult notion in Lua is that functions, like all other values, are anonymous; they do not have names. When we talk about a function name, say print, we are actually talking about a variable that holds that function. Like any other variable holding any other value, we can manipulate such variables in many ways. The following example, although a little silly, shows the point:"
Report Abuse
KEVEKEV77 is not online. KEVEKEV77
Joined: 12 Mar 2009
Total Posts: 6961
03 Sep 2017 01:07 AM
Btw why are you a mother still playing roblox.


By playing I mean all your badges, 196 friends, stuff like that.

Sry, I'm not a mother, and I know it dosen't disable you from doing what you want; I just mean generally you hang up the controller by then
Report Abuse
Thane_1 is not online. Thane_1
Joined: 08 Apr 2009
Total Posts: 827
03 Sep 2017 01:12 AM
No problem.



He's a dad btw, and he's helping his son.
Report Abuse
KEVEKEV77 is not online. KEVEKEV77
Joined: 12 Mar 2009
Total Posts: 6961
03 Sep 2017 01:57 AM
SAME APPLIES
Report Abuse
Thane_1 is not online. Thane_1
Joined: 08 Apr 2009
Total Posts: 827
03 Sep 2017 02:13 AM
But it doesn't apply cuz parents play with there kids all the time, and he's not just playing, he's helping his kid
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