I don't understand line 2 and 4. Also how does the use of parentheses work in this script, especially in line 2 and 6? Bonus question: what is the difference between CFrame and Vector3?
function onPlayerEntered(player) player.CharacterAdded:connect(function(char) --Anonymous function fired when player's character loads while wait() do -- Infinite loop! script.Parent.CFrame = CFrame.new(char.Torso.Position) -- Tele to player's position end end) end game.Players.PlayerAdded:connect(onPlayerEntered) -- connects function to PlayerAdded event.
UPDATE: Okay, this script was copied right out of ROBLOX's Scripting Book/Chapter 3: https://wiki.roblox.com/index.php?title=Scripting_Book/Chapter_3
I made an attempt of testing the script and it is not working. Yes, the script is placed under the part and the part is unanchored.
Some reading on CFrame from the Wiki
This property defines where the object is (its position), and how it is rotated (its orientation). The position information is also shown in the Position property, and the rotation information is shown in the Rotation property.
Basically, CFrame contains the Rotation and Position.
Vector3 is the Position and contains the X, Y, and Z.
The parentheses are for the parameters, the arguments that the function takes in. When the PlayerAdded event fires it automatically sends in the argument with the Player who joined. And for the CharacterAdded event the Character that loaded.
Anonymous function means it has no name. The reason line 6 has an end)
is because it's closing closing the function(end) and also the event CharacterAdded(parentheses).
You can also turn the onPlayerEntered
function into an anonymous function:
game.Players.PlayerAdded:Connect(function(ThePlayerWhoJoinedTheGame) ThePlayerWhoJoinedTheGame.CharacterAdded:Connect(function(TheCharacterBeingLoadedForSaidPlayer) while wait() do -- You will need to specify if your character models are R15 or R6 -- Mine is R15 so I use UpperTorso script.Parent.CFrame = CFrame.new(TheCharacterBeingLoadedForSaidPlayer.UpperTorso.Position) end end) end)