Menu
CoddyTech

Nhấp để phá

Bài học 6 trên 20 trong khóa học Trò chơi Roblox: mô phỏng thú cưng của Coddy.

ClickDetector phù hợp với một nút, nhưng hầu hết trò mô phỏng đều đọc thao tác nhấp chuột ở máy khách (client), để trò chơi có thể phản ứng ngay khi bạn nhấp (phát âm thanh, rung) trước khi máy chủ trả lời. Chuột của máy khách biết bạn đã nhấp vào đâu:

local mouse = me:GetMouse()
mouse.Button1Down:Connect(function()
	print(mouse.Target) -- phần tử tại vị trí nhấp chuột, hoặc nil
end)

Máy khách không thể tự phá đống xu: máy chủ quản lý các đống xu và tiền xu. Máy khách gửi yêu cầu qua RemoteEvent, một đối tượng được tạo ra để truyền tin giữa máy khách và máy chủ:

-- máy khách
remotes.Hit:FireServer(pile)
-- máy chủ: Roblox thêm player đã gửi sự kiện vào trước
remotes.Hit.OnServerEvent:Connect(function(player, pile)
end)

Tín hiệu từ remote đến trễ một chút, giống như khi truyền qua mạng. Trên điện thoại, thao tác chạm cũng tương tự như nhấp chuột. ClickDetector và dòng local hitPile trong bài học 3 sẽ bị bỏ: remote thay thế chúng.

challenge icon

Thử thách

Trung bình

Gửi lượt nhấp đến máy chủ thông qua RemoteEvent.

  1. Trong PetClient: local remotes = game.ReplicatedStorage:WaitForChild("Remotes"); local mouse = me:GetMouse(); khi mouse.Button1Down xảy ra, gọi remotes.Hit với mouse.Target nếu tên của nó là CoinPile.
  2. Trong PetServer: local remotes = game.ReplicatedStorage.Remotes; remotes.Hit.OnServerEvent gọi hitPile(player, pile).
  3. Xóa các dòng ClickDetector và local hitPile; viết local function hitPile.

Nhấn Chơi rồi nhấp vào một đống xu: nó vỡ như trước. Sau đó nhấn Kiểm tra.

Tự làm thử

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

local piles = workspace.Piles
local pileSpot = {} -- mỗi đống -> vị trí của nó

print("A Meadow pile gives " .. Settings.PileValue .. " coins")

-- Mỗi người chơi có Coins trên bảng xếp hạng
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)

-- Khi nhấp vào đống, code cần hitPile, được viết bên dưới spawnPile: khai báo tên
-- ở đây, trước cả hai
local hitPile

-- Một đống xu tại một vị trí: bản sao của ServerStorage.CoinPile, với máu
-- và giá trị là các thuộc tính
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)
	-- nhấp vào đống để đánh
	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

-- Mỗi lần đánh làm đống mất 1 máu;
-- khi máu về 0, đống vỡ và trả số xu bằng giá trị của nó
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()
	-- một đống mới xuất hiện tại cùng vị trí sau vài giây
	task.delay(Settings.RespawnTime, spawnPile, spot)
end

-- Mỗi vị trí trong Meadow có một đống
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
	spawnPile(spot)
end

Tất cả bài học trong Trò chơi Roblox: mô phỏng thú cưng

Tự luyện tập: Playground Roblox