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

I need help understanding this... not a request just a simple answer, please don't remove??!!

Asked by 6 years ago

So I have a question about arguments... I think that is what they are called.

game.Players.PlayerAdded:connect(function(plr)

So this is just a example of what I need help understanding. As you can see the plr part is there... I need help understanding that. What is it called (I think I know) and does it matter what it is. Also, is that the name of the function or what?? I am just confused some specifying would help a lot. THANKS.

0
connect is deprecated, switch to Connect. User#19524 175 — 6y
0
my message thingy said that you accepted my answer. Why then does it say you accepted @seith14? User#21908 42 — 6y
0
Can you fix that? User#21908 42 — 6y
0
@seith14 's answer is not even correct. plr is not a variable. It is a parameter. User#21908 42 — 6y
0
I was not looking for a correct way, I just needed help understanding, and @seith14 helped me. BossScorp82103 22 — 6y

6 answers

Log in to vote
0
Answered by
seith14 206 Moderation Voter
6 years ago
Edited 6 years ago

This Function doesn't have a name, It only has a Variable and no it doesn't matter what the variable "plr" is you could change it to robot and it would still be a player.

game.Players.PlayerAdded:connect(function(plr)

end)

Make sure you always close your )'S or it wouldn't know where the function would end and it would turn into an Error

0
By the way it is not a variable it is a parameter. User#21908 42 — 6y
0
Ah yes, but variables can be added from a remoteevent or from things inside another function seith14 206 — 6y
0
they are still parameters. Not variables User#21908 42 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

I dont know what you mean about this but its When the Player Just Joined Server?

0
and plr is player. User#21499 0 — 6y
0
If you don't have an answer post it in the comments. User#21908 42 — 6y
0
Answers can get downvoted, and it's not helpful for a comment to be an answer. seith14 206 — 6y
Log in to vote
0
Answered by
Eppobot 13
6 years ago

Basically "plr" is the player that joined the server. For example, if you use print(plr) it would print the name of the player.

game.Players.PlayerAdded:connect(function(plr)
    print(plr)
end)

---> name of the player.

0
you forgot plr.Name seith14 206 — 6y
0
@seith14 you can print() without using the .Name xJathur95x 129 — 6y
0
print only works with strings and numbers. It will not function correctly when you pass an object as the argument. User#21908 42 — 6y
0
I need help understanding what that extra litle () is for.... I know what plr means.... thanks though. BossScorp82103 22 — 6y
Log in to vote
0
Answered by 6 years ago

basically functions can return arguments that you can name to make this simpler i will make a function

function returnNumber(Number)-- this is a argument its unknown at the moment until it is called
return Number * 2--gives back the number times 2
end)

to call this we do something like this

function returnNumber(Number)
return Number * 2
end)


local Twenty = returnNumber(10) -- we give 10 and that goes back up to the function and replaces the Number variable with 10 

when you use game.Players.PlayerAdded:Connect it returns the actual player with the backpack the name and all the other properties in it

0
This is not how it works at all. Also please properly indent your code as it makes it easy to read and debug. Even if it is that short do it right. User#21908 42 — 6y
0
The PlayerAdded event is an event listener. It is not a function. It will run a function you provide it with. It in and of itself is not a function and it does not "return" player. User#21908 42 — 6y
Log in to vote
0
Answered by 6 years ago

First, make the "Connect" capitalized because connect is deprecated. And if you want it to run every time a player spawns, in between the first line and then "end)" put a

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        (code)
    end)
end)

and if you wanted it to happen to a specific player:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        if player.Name = "username here" then
        (code)
        end
    end)
end)

Hope it helped you.

Log in to vote
0
Answered by 6 years ago

Arguments are objects/values that are passed when you call a function that has parameters. Here is an example:

local function addNumbers(a, b)
    return a + b -- a and b are parameters when the function is called they will be added together and then returned
end

print(addNumbers(1, 2)) -- 1 and 2 are the arguments that are passed to the function a will be 1 and b will be 2

-- output will be three because 1 + 2 = 3

"What does this have to do with game.Players.PlayerAdded:Connect(function(plr)?" you might ask. Well it has a lot to do with it. In this script I will explain why:

local players = game:GetService("Players") -- GetService is the recommended method 

-- now we will explain the event:

players.PlayerAdded:Connect(function(plr) -- when an event happens like this one in roblox it checks for all scripts waiting for that event to happen then fires them/ starts the function up so basically we are creating a listener for that event to see when it happens. Then we are using connect to attach either a previously created function or an anonymous function to that event so it runs when the event is fired. The PlayerAdded event always has an argument like I mentioned earlier which is the player that joined. So we add a parameter in our function (if we want to) for that player argument that is passed. So plr is the player that joined. Here is an example of the Touched event with the script being in a part:

script.Parent.Touched:Connect(function(hit) -- whenever this event is fired the thing that hit whatever the touched event is attached to is passed as the argument and we have a parameter for it. Now you can connect an already created function like so:

local function onTouched(hit)
    print(hit.Parent.Name)
end

script.Parent.Touched:Connect(onTouched)
-- and the hit or whatever you name what touches the object is passed as the argument and you have a parameter ready for it. This does not mean that you need to have a parameter. You can just do this:

script.Parent.Touched:Connect(function
    print("I was touched.")
end)

-- same with all other events

I hope this helps and have a great day scripting!

0
Sorry for my sloppy answer I was in a hurry. User#21908 42 — 6y

Answer this question