Scripting Book/Chapter 3

From Legacy Roblox Wiki
Jump to navigationJump to search

Introduction

Wow! You're in chapter 3. Nice! This time we're going to learn how to make a brick follow you, then later on we're going to be making virtual pets that follow you everywhere you go in the game. However, that is much, much later!

Inserting the brick

First, we need to open up Explorer (View -> Explorer) and Properties (View -> Properties) windows make sure you're in ROBLOX Studio first!!

Now, we will need to insert the brick. Go to Insert -> Object -> Part -> OK.

Wow, that was fast! We have the brick already... Technicly it's called a part, but who cares!?

Making the brick follow us

Insert a script into the brick we just added Insert -> Object -> Script -> OK.

Make the script's source as followed:

function onPlayerEntered(player)
    player.CharacterAdded:connect(function(char) --Anonymous function fired when player's character loads
        while wait() do -- Infinate 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.

This way, whenever someone enters the game, the brick will follow them! Spooky.

That was an easy tutorial...

Go to previous chapter or Continue to Chapter 4