Thú cưng đào nhanh hơn
Bài học 13 trên 20 trong khóa học Trò chơi Roblox: mô phỏng thú cưng của Coddy.
Trong trò chơi mô phỏng thú cưng, thú cưng đào bới: mỗi thú cưng đang được thả ra sẽ cộng Power của nó vào mỗi lần đánh. Một con cáo (sức mạnh 4) khiến mỗi lần đánh gây ra 5 sát thương thay vì 1.
Cộng sức mạnh của những thú cưng đang được thả ra, rồi trừ 1 cộng với tổng đó khỏi máu của đống đồ. Thú cưng được cất đi sẽ không giúp ích.
Thử thách
Trung bìnhHãy để mọi thú cưng đang đi theo cùng đào với bạn.
- Viết hàm
petPower(player): tính tổng các thuộc tínhPowercủa thú cưng đang được trang bị. - Trong
hitPile, trừ1 + petPower(player)khỏi máu thay vì 1.
Nhấn Chơi, ấp một thú cưng rồi đánh một đống xu: con số trên nhãn giảm 2 trở lên. Sau đó nhấn Kiểm tra.
Tự làm thử
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local RunService = game:GetService("RunService")
local Settings = require(game.ReplicatedStorage.Settings)
local remotes = game.ReplicatedStorage.Remotes
local piles = workspace.Piles
local petParts = workspace.PetParts
local pileSpot = {} -- Mỗi đống xu: vị trí đặt đống xu đó
local lastHit = {} -- Mỗi người chơi: thời điểm họ đánh gần nhất
print("A Meadow pile gives " .. Settings.PileValue .. " coins")
-- Settings.Pets theo tên
local petInfo = {}
for _, info in Settings.Pets do
petInfo[info.Name] = info
end
-- Thú cưng: một StringValue trong thư mục Pets của người chơi, lưu tên thú cưng, cùng
-- độ hiếm và sức mạnh dưới dạng thuộc tính
local function givePet(player, name, equipped)
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:SetAttribute("Equipped", equipped)
pet.Parent = player.Pets
return pet
end
-- Số thú cưng mà người chơi đang cho ra ngoài
local function equippedCount(player)
local count = 0
for _, pet in player.Pets:GetChildren() do
if pet:GetAttribute("Equipped") then
count += 1
end
end
return count
end
-- Bài học 13: Thú cưng đào nhanh hơn sẽ nằm ở đây
-- Mỗi người chơi đều 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
-- Những thú cưng họ sở hữu
local pets = Instance.new("Folder")
pets.Name = "Pets"
pets.Parent = player
end)
-- Đống xu tại một vị trí: bản sao của ServerStorage.CoinPile, có lượng máu
-- và giá trị dưới dạng 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)
pile.HealthGui.Label.Text = pile:GetAttribute("Health") .. "/" .. pile:GetAttribute("MaxHealth")
pileSpot[pile] = spot
pile.Parent = piles
return pile
end
-- Mỗi lần đánh làm đống xu mất 1 máu;
-- Khi về 0, đống xu vỡ ra và trả số xu bằng giá trị của nó
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()
-- Vài giây sau, một đống xu mới xuất hiện ở cùng vị trí
task.delay(Settings.RespawnTime, spawnPile, spot)
end
-- Mỗi vị trí trong Meadow có một đống xu
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
spawnPile(spot)
end
-- Máy khách yêu cầu đánh đống xu. Đừng tin máy khách: bất kỳ ai cũng có thể kích hoạt
-- remote này với bất kỳ dữ liệu nào, từ bất kỳ đâu, nhanh tùy ý.
remotes.Hit.OnServerEvent:Connect(function(player, pile)
-- đúng là một đống xu
if typeof(pile) ~= "Instance" or pile.Parent ~= piles then
return
end
-- trong tầm với
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
-- không quá nhanh
local now = time()
if lastHit[player] and now - lastHit[player] < Settings.HitCooldown then
return
end
lastHit[player] = now
hitPile(player, pile)
end)
-- Rút thăm có trọng số: Chance của mỗi thú cưng, tính trên 100, là phần của nó trên dải xác suất
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
-- Ấp trứng: dùng lời nhắc của quả trứng và trả bằng xu
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()
-- cho ra ngoài ngay nếu còn chỗ
local pet = givePet(player, name, equippedCount(player) < Settings.MaxEquipped)
print(player.Name .. " hatched a " .. name)
end)
-- Thú cưng đang ra ngoài đi theo chủ: mỗi con có một bộ phận trong thế giới, luôn nằm
-- thành một hàng phía sau người chơi ở mỗi khung hình
local petModels = {} -- mỗi thú cưng -> bộ phận tương ứng trong thế giới
RunService.Heartbeat:Connect(function()
for _, player in Players:GetPlayers() do
local root = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
local pets = player:FindFirstChild("Pets")
local slot = 0
for _, pet in pets and pets:GetChildren() or {} do
local model = petModels[pet]
if pet:GetAttribute("Equipped") and root then
slot += 1
if not model then
model = ServerStorage.PetModels[pet.Value]:Clone()
model.Parent = petParts
petModels[pet] = model
end
model.CFrame = root.CFrame * CFrame.new((slot - 2) * 3, -1.5, 4)
end
end
end
end)
Tất cả bài học trong Trò chơi Roblox: mô phỏng thú cưng
3Trứng và thú cưng
Ấp trứngTự luyện tập: Playground Roblox