Why is the parameter in this script “player”, and how does it function in this script?
local Players = game:GetService("Players")
local function onPlayerAdded(player)
-- Create a container for leaderstats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
-- Create one leaderstat value
local vScore = Instance.new("IntValue")
vScore.Name = "Score"
vScore.Value = 0
vScore.Parent = leaderstats
-- Add to player (displaying it)
leaderstats.Parent = player
end
Players.PlayerAdded:Connect(onPlayerAdded)
onPlayerAdded always gives a parameter player (player who joined). You can use this only inside this function. You can see it as a variable that you can use but can’t change
Hello!
Once a player is added to the game, the function onPlayerAdded() Fires passing the variable player which is the player that joined into the game. The function then creates a folder in leaderstats, and then it inserts an intValue and applies the name “Score” and sets the value to 0. This function essentially adds the player onto the games leaderboard by using roblox inbuilt leaderboard system.
This article explains what that function does essentially.
All events or RBXScriptSignal have that. Imagine that an event has a property called Connection. What Connect() does is set that property to the function in its parameter.
local Conection = Event.Connection
print(Connection) -- prints "nil"
Event:Connect(Func)
print(Connection) -- prints the memory address of the function Func()
Here’s how Connect() sets Connection
function Event:Connect(func)
self.Connection = func -- same as Event.Connection = func
end
Do take note that Event.Connection is just imaginary and really doesn’t exist. If you ever use it in an event such as Players.PlayerAdded it will return nil or will error.
How does the function actually run?
Think of it this way. When a player joins the game, the core scripts (probably) fire the event Players.PlayerAdded by Event:Fire(). What Fire() does is that it runs Event.Connection (keep in mind this is imaginary). Here’s what it looks.
-- when a player joins the game
function Event:Fire(player: Player)
self.Connection(player) -- same as Event.Connection(player)
end
Also, you can’t access Event:Fire() either it is only used by CoreScripts or it really doesn’t exist at all and the CoreScripts/Roblox Player has an alternative way to fire events like Players.PlayerAdded.
Feel free to correct me if I’m wrong on something. I’m aware that you can connect multiple functions in an event but I kept it this way so it’s easy to understand as possible.
There’s some preset events, that run when something specific happens. This condition is predetermined. They may also pass specific pieces of data. When you write out the event, the intellisense will show you what it will pass. It’s things like:
-- The event PlayerAdded will always pass the specific player that was just added. Say I want to print a welcome with their name:
game.Players.PlayerAdded:Connect(function(player)
print("Welcome, " .. player.Name)
end)
Other events pass other things.
Eamples:
When the following events happen,
UserInputService.InputBegan passes “input” object (contains info such as what key was pressed)
Runservice.RenderStepped passes “dt” object (delta time, the difference between the previous step and the current
Part.GetAttributeChangedSignal passes the name of the attribute that was changed.
So the event returns a player. Therefore, the parameter inside the function will always be a player. It doesn’t have to be named player, it can be named whatever you’d like, but it’ll be a player.
In this script, the parameter “player” is used as a placeholder for an argument that will be passed to the function when it is called. Let’s break down how it functions in the script:
Defining the Function:
local function onPlayerAdded(player)
Here, a function named onPlayerAdded is defined, and it takes one parameter called player. This means that whenever this function is called, it expects to receive a single argument, which will be assigned to the variable player within the function.
Inside the Function:
Within the function, several actions are taken using the player parameter:
A new folder named “leaderstats” is created using Instance.new("Folder"). This folder will be used to store player statistics.
A new IntValue named “Score” is created using Instance.new("IntValue"). This IntValue represents the player’s score, and it is initially set to 0.
The “Score” IntValue is parented to the “leaderstats” folder, which in turn is parented to the player.
Connection to PlayerAdded Event:
The script then establishes a connection to the PlayerAdded event of the Players service using the Players.PlayerAdded:Connect(onPlayerAdded) line. This means that whenever a player joins the game, the onPlayerAdded function will be called, and the player who joined will be passed as an argument to the function.
So, the player parameter serves as a reference to the player who has just joined the game. It allows you to perform actions and make modifications specific to that player, such as creating a “leaderstats” folder for them and initializing their score with an IntValue. This is a common pattern in Roblox game development to set up player-specific data and functionality when players join the game.
If they don’t understand, they don’t understand. I will explain it in a different way until they do. Please stop discouraging people from asking questions.
roblox will pass back certain parameters because otherwise it’d be difficult to make it work, touched event returns the part touched and player added returns the player that joined. some events don’t pass back anything like textbutton.MouseButton1Click, To easily find what roblox returns you can go to the roblox API page, it usually shows in the parameters section, or you can print whats in the parenthesis like