Coins on Screen
Lesson 5 of 20 in Coddy's Roblox Game: Pet Simulator course.
Now the client. PetClient is a LocalScript inside the HUD, a ScreenGui that Roblox copies from StarterGui into each player's screen when they spawn. A LocalScript runs on that player's own device, and it can name the player it runs for:
local me = game:GetService("Players").LocalPlayerIn a server Script, LocalPlayer is nil: the server serves every player at once.
The server puts leaderstats into the player a moment after they join, so the client waits for it with WaitForChild. Then it shows the coins in the HUD's Coins label and again every time they change: an IntValue's Changed fires with the new value.
The label only shows what the server decided. Changing Coins.Value in a LocalScript would change this player's view of it and nothing on the server.
Challenge
MediumShow the player's coins on their screen.
- In
PetClient:local Players = game:GetService("Players"),local me = Players.LocalPlayer,local hud = script.Parent. local coins = me:WaitForChild("leaderstats"):WaitForChild("Coins")- Write
showCoins():hud.Coins.Text = "Coins: " .. coins.Value. Call it once and connect it tocoins.Changed.
Press Play and break a pile: the top left reads Coins: 5. Then Check.
Try it yourself
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local piles = workspace.Piles
local pileSpot = {} -- each pile -> the spot it stands on
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 pile's click needs hitPile, which is written below spawnPile: name it
-- here, before either
local hitPile
-- 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)
-- click it to hit it
local detector = Instance.new("ClickDetector")
detector.Parent = pile
pileSpot[pile] = spot
pile.Parent = piles
detector.MouseClick:Connect(function(player)
hitPile(player, pile)
end)
return pile
end
-- A hit takes 1 off a pile's health;
-- at 0 the pile breaks and pays its value
function hitPile(player, pile)
local health = pile:GetAttribute("Health") - 1
if health > 0 then
pile:SetAttribute("Health", health)
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
All lessons in Roblox Game: Pet Simulator
Practice on your own: Roblox playground