A Pet Follows You
Lesson 11 of 20 in Coddy's Roblox Game: Pet Simulator course.
Pets should follow you around. A hatched pet comes out at once, marked with an Equipped attribute, and gets a part in the world: a copy of its model from ServerStorage.PetModels.
Every frame the server puts that part behind its owner. RunService.Heartbeat fires once a frame, and multiplying CFrames places one relative to another:
part.CFrame = root.CFrame * CFrame.new(0, 2, 6) -- 6 studs behind, 2 upA table keyed by the pet remembers its part, so each pet gets one part, made once:
if not petModels[pet] then
petModels[pet] = ServerStorage.PetModels[pet.Value]:Clone()
petModels[pet].Parent = petParts
endChallenge
MediumHatched pets follow you.
- Give
givePeta third argument,equipped, stored as theEquippedattribute; hatchgivePet(player, name, true). - Add
RunService,local petParts = workspace.PetParts,local petModels = {}. - On
RunService.Heartbeat, give each Equipped pet one clone ofServerStorage.PetModels[pet.Value]in petParts (kept inpetModels[pet]) and set its CFrame toroot.CFrame * CFrame.new(0, -1.5, 4).
Press Play, hatch and walk: your pet trails you. Then Check.
Try it yourself
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local remotes = game.ReplicatedStorage.Remotes
local piles = workspace.Piles
local pileSpot = {} -- each pile -> the spot it stands on
local lastHit = {} -- each player -> when they last hit
print("A Meadow pile gives " .. Settings.PileValue .. " coins")
-- Settings.Pets by name
local petInfo = {}
for _, info in Settings.Pets do
petInfo[info.Name] = info
end
-- A pet: a StringValue in the player's Pets folder holding its name, with
-- its rarity and power as attributes
local function givePet(player, name)
local info = petInfo[name]
local pet = Instance.new("StringValue")
pet.Name = "Pet"
pet.Value = name
pet:SetAttribute("Rarity", info.Rarity)
pet:SetAttribute("Power", info.Power)
pet.Parent = player.Pets
return pet
end
-- Every player gets Coins on the leaderboard
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
-- the pets they own
local pets = Instance.new("Folder")
pets.Name = "Pets"
pets.Parent = player
end)
-- A coin pile on a spot: a copy of ServerStorage.CoinPile with its health
-- and value as attributes
local function spawnPile(spot)
local pile = ServerStorage.CoinPile:Clone()
pile.Position = spot.Position
pile:SetAttribute("MaxHealth", Settings.PileHealth)
pile:SetAttribute("Health", Settings.PileHealth)
pile:SetAttribute("Value", Settings.PileValue)
pile.HealthGui.Label.Text = pile:GetAttribute("Health") .. "/" .. pile:GetAttribute("MaxHealth")
pileSpot[pile] = spot
pile.Parent = piles
return pile
end
-- A hit takes 1 off a pile's health;
-- at 0 the pile breaks and pays its value
local function hitPile(player, pile)
local health = pile:GetAttribute("Health") - 1
if health > 0 then
pile:SetAttribute("Health", health)
pile.HealthGui.Label.Text = health .. "/" .. pile:GetAttribute("MaxHealth")
return
end
player.leaderstats.Coins.Value += pile:GetAttribute("Value")
local spot = pileSpot[pile]
pileSpot[pile] = nil
pile:Destroy()
-- a new pile on the same spot, a few seconds later
task.delay(Settings.RespawnTime, spawnPile, spot)
end
-- A pile on every spot in the Meadow
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
spawnPile(spot)
end
-- The client asks to hit a pile. Never trust the client: anyone can fire
-- this remote with anything, from anywhere, as fast as they like.
remotes.Hit.OnServerEvent:Connect(function(player, pile)
-- a real pile
if typeof(pile) ~= "Instance" or pile.Parent ~= piles then
return
end
-- in reach
local character = player.Character
local root = character and character:FindFirstChild("HumanoidRootPart")
if not root or (root.Position - pile.Position).Magnitude > Settings.Reach then
return
end
-- not too fast
local now = time()
if lastHit[player] and now - lastHit[player] < Settings.HitCooldown then
return
end
lastHit[player] = now
hitPile(player, pile)
end)
-- A weighted roll: each pet's Chance (out of 100) is its share of the line
local function rollPet()
local roll = math.random() * 100
for _, info in Settings.Pets do
roll -= info.Chance
if roll <= 0 then
return info.Name
end
end
return Settings.Pets[1].Name
end
-- Hatching: the egg's prompt, paid for in coins
workspace.Egg.ProximityPrompt.Triggered:Connect(function(player)
local coins = player.leaderstats.Coins
if coins.Value < Settings.EggPrice then
return
end
coins.Value -= Settings.EggPrice
local name = rollPet()
givePet(player, name)
print(player.Name .. " hatched a " .. name)
end)
-- Lesson 11: A Pet Follows You goes here
All lessons in Roblox Game: Pet Simulator
3Eggs and Pets
Hatch an EggPractice on your own: Roblox playground