Question about Parameters

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)
2 Likes

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

1 Like

You can create a function inside the event, to make things easier:

Before:

function playerAdded(player)
    print(player)
end

game.Players.PlayerAdded:Connect(playerAdded)

After:

game.Players.PlayerAdded:Connect(function(player)
    print(player)
end)
1 Like

Because the RBXScriptSignal takes a function as a parameter and calls it with the intended parameters after the event occurs.

Idk what you mean by this, how does player function?

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.

It’s a great explanation, I just cant wrap my mind around it 100%

if you are wondering why the script is not working u should make leaderstats.Parent = player

Would the parameter have to be player? or could it be something else? or isn’t it like a parameter of the event itself

no, yes, yes this is a spoilerchars

It’s nothing complicated, it’s just a parameter an event gives out when fired, like this:

game.Players.PlayerAdded:Connect(function(player)
    print(player.Name)
end)

Also how regular functions return parameters, something like this:

function upperCase(str)
    return string.upper(str)
end

local text = upperCase("hello world")
print(text)

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.

game.Players.PlayerAdded:Connect(function(lolmyparameter)
    print(lolmyparameter.Name)
end)

Or:

function myFunction(parameter)
print(parameter.Name)
end

game.Players.PlayerAdded:Connect(myFunction)
  1. When a player is added, run function ‘myFunction’.
  2. PlayerAdded always passes the player in question that just joined.
  3. Therefore, ‘parameter’ will be the player who’s just joined
2 Likes

This was already explained by so many other people, including @ExercitusMortem.

Stop asking for more details are there really isn’t any more left to cover about parameters.

1 Like

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:

  1. 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.

  2. 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.
  3. 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.

1 Like

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.

1 Like

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

part.Touched:Connect(function(x)
      print(x)
end)
1 Like

This is also a second solution

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.