Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Can someone please explain this script in layman's terms?

Asked by
RAYAN1565 691 Moderation Voter
6 years ago
Edited 6 years ago

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.

1
before the word function, you can see a open parenthesis. In line 6, you use a close parenthesis to close the function hellmatic 1523 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

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)
1
Or just `TheCharacterBeingLoadedForSaidPlayer.UpperTorso.CFrame` SebbyTheGODKid 198 — 6y
1
Yes, but I didn't want to change the script too much MooMooThalahlah 421 — 6y
1
What's R15 or R6? RAYAN1565 691 — 6y
0
The Roblox model you're using in the game. You can set it in the settings to allow only R6 or R15 models, or both. MooMooThalahlah 421 — 6y
Ad

Answer this question