I’d definitely recommend you check out a few documentation posts as you go through whatever tutorials you decide to check out.
This documentation for the Touched
event in relation to two BaseParts
touching shows that there is a parameter being passed called otherPart
.
This sample code:
game.Workspace.Part.Touched:Connect(function(otherPart)
print("Touched " .. otherPart.Name)
end)
Is relatively the same as this code here:
local function onTouched(touchedPart)
print("Touched " .. touchedPart.Name)
end
game.Workspace.Part.Touched:Connect(onTouched)
Both of these snippets will produce the same outcome, which is printing to the console “Touched [name of part]”.
These are built-in events, which in this case, when two parts touch each other the Touched event is fired and the parameter otherPart
is also passed along with it. In the function that you connect it to, like onTouched
, you can rename that parameter to be anything you want, like touchedPart
.
In summary, roblox is giving us the other part that was touched with no extra work on our end, that’s what built-in events are and there are many more for you to find out and have fun with, they are essential to roblox developing.
To go onto the topic of this:
local PlayerAdded(player: Player)
-- Some code
end
game:GetService("Players").PlayerAdded:Connect(PlayerAdded)
This code should really be reformatted to look like this for best practice and to fix that function mistake:
local Players = game:GetService("Players")
local function onPlayerAdded(player)
-- Some code
-- ex: print(player.Name) -- This would be the player who just joined the game
end
Players.PlayerAdded:Connect(onPlayerAdded)
player: Player
is optional here in my opinion since that is what’s called type checking. You aren’t going to need type checking quite yet but feel free to make it a habit if you so please.
This is another built-in event of the Players Service. Services are something you should also get familiar with possibly using the documentation, or even looking up a tutorial specifically related to them but they are fairly straightforward.
All you have to know for this is every time a player joins, the PlayerAdded
event is fired as a part of the Players Service, so when you want to do something when a player joins, that’s your built-in event.
I hope this helped out and I tried not to use too much complicated terminology, feel free to ask more questions of course!