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

Can someone explain arguments and parameters in a function to me ?

Asked by 9 years ago

I don't understand it because in this particular script, "hit" is not repeated in the body:

function onTouched(hit) local t = script.Parent:findFirstChild("Torso") if t ~= nil then t.CFrame = CFrame.new(27, 40, 2)--input position in the () end end

script.Parent.Touched:connect(onTouched)

Btw this doesn't work, and I got it from a model.

2 answers

Log in to vote
1
Answered by 9 years ago

Sorry, you should use a Code Block next time when posting code because this is very hard to read. It's the little Lua Bubble.

Like this

Anyways, I am still pretty new to scripting but i'll give this my best shot. A Parameter is a variable inside a function. And an argument is the Value that this Variable is set to. Here:

-- number is the parameter.
function blah(number)
 print(number)
end

blah(2) -- 2 is the argument.

You should check the wiki for this type of stuff its got all sorts of info.

0
OHHHH SO IT WOULD PRINT 2? So what role does it play in my script? And btw thx for the code block thingy. :3 BlizzardAwesomeNinja 10 — 9y
0
I'm not to sure but from the looks of things, it has no role. xD I don't see it called or used at all in the script. :P CrispyBrix 113 — 9y
0
Not sure what hit would do I mean, I don't think anything. But yes it would print 2. :3 CrispyBrix 113 — 9y
Ad
Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

I'm just going to answer your question under Nexus's answer - there's not enough room in the comment feature.


You probably noticed that number has to be assigned a value when the function is called on line 6. If you don't assign it a value, it just equals nil.

function blah(number)
    print(number)
end
blah()
    --Output: nil

In your case, Nexus assigned it the integer 2, causing the output to print 2.

Some events, however, have built in parameters that you don't need to assign values to.These parameters will always equal the same thing.

Touched events have a single built in parameter equal to the part that did the touching. This is commonly named hit, because it's the part that hit the other part, but you can name it whatever you want. For example, if we placed the following code inside a brick,

function whenITouchThisBrick(partThatDidTheTouching)
    print(partThatDidTheTouching.Name)
end
script.Parent.Touched:connect(whenITouchThisBrick)

if your Left Leg touched the part that had this code inside it, the output would print "Left Leg". Notice that we never assigned an argument to the parameter partThatDidTheTouching. We only connected the function to the Touched event. This is okay, however, because the Touched event will automatically give us the argument.


It plays no role in your script because you never use it.

Answer this question