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.
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
I dont know what you mean about this but its When the Player Just Joined Server?
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.
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
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.
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!