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

Need help with how arguments in function parameters work?

Asked by 7 years ago

Alright so this is probably a simple question on this site but how do arguments work in function parameters? I'm a bit confused with this still and I just need a better explanation of it.

function onPlayerEntered(nP)
nP.Chatted:connect(function(msg)
m = Instance.new("Message", game.Workspace)
m.Text = nP.Name.. ": " ..msg
wait(3)
m:remove()
end)
end

game.Players.PlayerAdded:connect(onPlayerEntered)

Yes this is from the scripting book but the argument is nP and I'm still a bit confused on what it does with nP.Chatted:connect(function(msg) and other things like that. I looked at other wiki's and I just need a better explanation.

3 answers

Log in to vote
1
Answered by
Zeloi 40
7 years ago

nP means Player, you can find what roblox function parameters are via the roblox wiki

I.E: http://wiki.roblox.com/index.php?title=API:Class/Humanoid

On that wiki page you can see in the events area you can see what arguments they 'give.'

So if we look at Humanoid.Running we see that the event gives one argument, a "float" value, that shows the humanoids' speed.

And if we look at Humanoid.Died we see that the event gives no arguments.

If we go back the the running event we can use the arguments we receive from connecting the function like so..

function running(speed)
       print(speed)
end

Humanoid.Running:connect(running)

This function shows the humanoids' speed once you start or stop running.

If you are still confused you can ask me any questions you have.

0
Alright thanks I'm starting to understand it a bit more. If I do need more help about something related to this would you mind if I PMed you through roblox? Retr0Thief 104 — 7y
0
No, I wouldn't mind! Zeloi 40 — 7y
0
Ahh I was assinging it like local var = ... not {...} connor12260311 383 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

Parameters are quite simple actually. parameters are used in a function and separated by commas, like so

function(p, p1, p2)
--or
function(p)

They are used in functions as variables you can change within the function, like so

function printNums(n1,n2,n3)    
    print(n1..n2..n3)
end

printNums(4,5,6)

You can change n1, n2, and n3 to any variable including string boolean or integer. For example is I replaced n1, n2, and n3 with 4, 5, and 6, this would appear in the output: 456 there is also the ... parameter which means you can have as many variables as you want. You access them by using an ipairs statement or something. (I'm not even sure how to use them)

1
You can use the ... parameter by doing local var = {...} and then all of the arguments are now in the table... then you can use a for loop on it and access every argument. Zeloi 40 — 7y
Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
7 years ago

There is a lot going on in that code block, so I'm going to go over each piece.

ROBLOX Events (Touched, KeyDown, RenderStepped, etc.) use callbacks to function. These callbacks are passed specific arguments depending on the event. For instance, Touched passed the part that caused the Touched event to fire.

The syntax for setting up an event callback has two forms, both of which are used in your code block:

nP.Chatted:connect(function(msg) --< here

game.Players.PlayerAdded:connect(onPlayerEntered) --< and here

The first is anonymous, the second, named. This works due to something called first-class functions, but that is beyond the scope of this answer.

The difference to notice is that the anonymous version has the function defined inside of the call to connect, whereas the named version has it defined elsewhere, and passed in like any other variable.


In the case of the PlayerAdded event, only one this is returned: the Player object that is created when the event has been fired. Note the difference in spelling from the Players service.

The Player object has many methods and events, one of which is Chatted. As indicated on the linked page, it returns two things: the message being sent, and the Player object of the 'recipient'. The latter will always be nil unless the whisper functionality of ROBLOX chat it used, in which case it will be the Player whispered.


To summarize: PlayerAdded passes in the Player object that was added when the event fired to the given callback function, which is OnPlayerEntered.

This Player object is captured using the parameter nP, and itself has an event called Chatted that is given an anonymous callback. This anonymous function is passed the message being said, and the Player object, if any, being whispered.


If you ever come across an unfamiliar event, look it up on the wiki! If that fails, definitely ask some questions here.

Answer this question