Hatch an Egg
Lesson 9 of 18 in Coddy's Roblox Game: Pet Simulator course.
The white egg near the spawn has a ProximityPrompt: walk up to it and Roblox shows "Hatch" with a key to hold (E, or a tap on a phone). When the player holds it long enough, the prompt fires Triggered on the server, with the player:
workspace.Egg.ProximityPrompt.Triggered:Connect(function(player)
end)The server checks the price, takes the coins and gives a pet. A pet is a StringValue holding its name, in a Pets folder inside the player. Everything the server puts inside a player replicates to that player's client, which is how the pet list in a later lesson knows what to show.
Challenge
Medium- When a player joins, also give them a Folder named
Pets. - Write
givePet(player, name): a StringValue namedPet, Value the name, in the player's Pets; return it. - Connect
workspace.Egg.ProximityPrompt.Triggered: if the player has fewer coins thanSettings.EggPrice, stop; otherwise take the price, give them aCat, and printPlayer1 hatched a Cat(their Name).
Press Play, earn 25 coins, and hold E at the egg. 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")
-- 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
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)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Roblox Game: Pet Simulator
3Eggs and Pets
Hatch an Egg