Click to Break
Lesson 6 of 18 in Coddy's Roblox Game: Pet Simulator course.
A ClickDetector is fine for a button, but most simulators read clicks on the client, so the game can react the moment you click (a sound, a shake) before the server answers. The client's mouse knows what it clicked:
local mouse = me:GetMouse()
mouse.Button1Down:Connect(function()
print(mouse.Target) -- the part under the click, or nil
end)The client cannot break a pile itself: the server owns the piles and the coins. It asks, through a RemoteEvent, an object built for messages between client and server:
-- client
remotes.Hit:FireServer(pile)
-- server: Roblox adds the player who sent it, first
remotes.Hit.OnServerEvent:Connect(function(player, pile)
end)A remote arrives a moment later, as over a network. On a phone a tap is the same click.
Challenge
Medium- In
PetClient:local remotes = game.ReplicatedStorage:WaitForChild("Remotes"). On the mouse'sButton1Down, ifmouse.Targetis a part namedCoinPile, fireremotes.Hitwith it. - In
PetServer:local remotes = game.ReplicatedStorage.Remotes, connectremotes.Hit.OnServerEventto callhitPile(player, pile), and remove the ClickDetector fromspawnPile(and thelocal hitPileline: writelocal function hitPileunder spawnPile instead).
Press Play and click a pile. 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.