Ok so you know that this script is adding sparkles to head, but how exactly is it being done?
The first thing I want you to get is how a function works, the code in your post contains two functions, and they are:
local function onCharacterAdded(character)
if not character:FindFirstChild("Sparkles") then
local sparkles = Instance.new("Sparkles")
sparkles.Parent = character:WaitForChild("Head")
end
end
local function onPlayerAdded(player)
if player.Character then
onCharacterAdded(player.Character)
end
player.CharacterAdded:Connect(onCharacterAdded)
end
So the code located with the functions onPlayerAdded and onCharacterAdded are only executed when we call that function. For example, in this line here:
for i, player in pairs(Players:GetPlayers()) do
onPlayerAdded(player)--calling the onPlayerAdded function
end
--Also here:
Players.PlayerAdded:Connect(onPlayerAdded)
--[[
As you can see the onPlayerAdded function gets called each time a new player is added
You may be wondering how the player gets passed into the function, so the PlayerAdded
event is know as an script signal which is made by roblox, and when these
signals fires, some of them pass along data which is very useful. To find out about
these signals and the information they pass you must use the Devhub which contains
all the relevant info about each class and well all all of its various events(signals) and functions. Here is a link to the PlayerAdded event:
https://developer.roblox.com/en-us/api-reference/event/Players/PlayerAdded
]]--
To help you understand how this works Im going to show you another way in which this could
have been written:
game.Players.PlayerAdded:Connect(function(Player)
print(Player.Name)
end)
--does the same thing as:
local function OnPlayerAdded(Player)
print(Player.Name)
end
game.Players.PlayerAdded:Connect(OnPlayerAdded)
--so this line of code is just running the function each time a player is added
Ok so know lets explain each of the individual functions in greater detail:
local function onPlayerAdded(player)
--[[
ok so this function takes one argument
that represents the Player object,
an argument refers to values
that are declared or stated within the function header.
]]--
if player.Character then -- this line is checking if the player character is loaded in
--if it is loaded in then we call the function
onCharacterAdded(player.Character)
end
--[[
if it is not loaded in then the script will wait until it loads
in before calling the onCharacter function,the CharacterAdded event
is a signal of the Player class that fires when a player's character
is added to the game. This must be done because
some players may have a bad connection and thus there characters might take
sometime to load in. Also, on devhub it states that the CharacterAdded event
passes as the character that is loaded in when the signal gets fired:
here is a link to event:
https://developer.roblox.com/en-us/api-reference/event/Player/CharacterAdded
--]]
player.CharacterAdded:Connect(onCharacterAdded)
end
local function onCharacterAdded(character)
--this function takes a specific player's character as the argument
if not character:FindFirstChild("Sparkles") then--this line is checking if the player does no have a child named Sparkles
--if it does not we add the sparkles to the head
local sparkles = Instance.new("Sparkles")
sparkles.Parent = character:WaitForChild("Head")
end
end
Ok so next lets go to the loop:
for i, player in pairs(Players:GetPlayers()) do
onPlayerAdded(player)
end
--[[
This loop is basically going over all the players in the game
and calling the function OnPlayerAdded on each player so that
every player in game can get Sparkles added to their heads.
I think the reason we have to do this is because sometimes when
new servers are made players join before all the code gets executed, however I'm not sure
but this just ensures that the script gets every player.
]]--
Lastly and example of how this works when a new player joins:
- Player A joins, as such the PlayerAdded event in fired by roblox and this line will basically
get fired:
game.Players.PlayerAdded:Connect(OnPlayerAdded)
-
The OnPlayerAdded function will run
-
The OnCharacterAdded function will fun and sparkles will get added to the player