Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How does the function work without any defining?

local Players = game:GetService("Players")
 
local function onCharacterAdded(character)
	print(character.Name .. " has spawned")
end
 
local function onCharacterRemoving(character)
	print(character.Name .. " is despawning")
end
 
local function onPlayerAdded(player)
	player.CharacterAdded:Connect(onCharacterAdded)
	player.CharacterRemoving:Connect(onCharacterRemoving)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)

How is it that it prints character.Name when character is not defined????

2 answers

It is indirectly defined.

PlayerAdded >> threaded to fire characteradded & automatically passing player CharacterAdded >> threaded to fire when the automatically passed variable's character spawns.

Variables are just being passed in a non-visual manner via stringing multiple functions together.

#Parameters and Arguments

A function's parameter is used when you define the function.

For example:

function multiply(x,y) -- These are the parameters. X and Y in this case(you  can name them whatever you want. In your case it was "character"
	return x*y
end

When you call the function, you use arguments, which will be assigned to the parameters

multiply(2,5) -- 2 and 5 are arguments.

Output -- 10

This discussion is preserved as it was. New answers, comments, and votes are closed.