Menu
CoddyTech

你的宠物列表

Coddy Roblox 游戏:宠物模拟器 课程的第 14 课,共 20 课。

玩家需要看到自己的宠物。HUD 中有一个 Inventory ScrollingFrame,里面有一个 UIListLayout,可以按 LayoutOrder 将其中的内容从上到下排列。每只宠物都会有一个 TextButton。它的 AutomaticCanvasSize 是 Y,因此画布会随着列表增长,宠物很多的玩家可以滚动查看。

客户端根据服务器保存在玩家身上的 Pets 文件夹构建列表,并在内容发生变化时重新构建:宠物孵化时触发 ChildAdded,宠物装备或卸下时触发各自的 GetAttributeChangedSignal("Equipped")。

pet:GetAttributeChangedSignal("Equipped"):Connect(showPets)

重新构建时,先移除旧按钮,这样列表中就不会重复显示同一只宠物。

challenge icon

挑战

中等

在屏幕上列出你的宠物,每只宠物对应一个按钮。

  1. 在 PetClient 中:local pets = me:WaitForChild("Pets")。
  2. 编写 showPets():销毁 hud.Inventory 中的 TextButton,然后为每只宠物添加一个按钮。如果宠物已装备,按钮显示 Cat (out);如果未装备,则显示 Cat。
  3. 现在调用它,并在 pets.ChildAdded 时调用;对于当前已有和之后新增的宠物,也在每只宠物的 GetAttributeChangedSignal("Equipped") 时调用。

按下 Play 并孵化宠物:“Cat (out)”会显示在列表中。然后点击 Check。

亲手试试

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 = {} -- 每个钱币堆对应它所在的位置
local lastHit = {} -- 每个玩家对应上次攻击的时间

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

-- 按名称查找 Settings.Pets 中的宠物
local petInfo = {}
for _, info in Settings.Pets do
	petInfo[info.Name] = info
end

-- 宠物:玩家的 Pets 文件夹中的一个 StringValue,保存着宠物的名称,
-- 并将稀有度和力量作为属性
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

-- 玩家当前派出了多少只宠物
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

-- 玩家的宠物每次攻击额外增加的伤害
local function petPower(player)
	local power = 0
	for _, pet in player.Pets:GetChildren() do
		if pet:GetAttribute("Equipped") then
			power += pet:GetAttribute("Power")
		end
	end
	return power
end

-- 每个玩家的排行榜上都有 Coins
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

	-- 他们拥有的宠物
	local pets = Instance.new("Folder")
	pets.Name = "Pets"
	pets.Parent = player
end)

-- 位置上的钱币堆:ServerStorage.CoinPile 的副本,生命值
-- 和值作为属性保存
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

-- 每次攻击会使钱币堆的生命值减少 1,再加上玩家宠物的力量;
-- 生命值降到 0 时,金币堆会碎掉,并发放对应的金币
local function hitPile(player, pile)
	local health = pile:GetAttribute("Health") - (1 + petPower(player))
	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()
	-- 几秒后在同一位置生成新的金币堆
	task.delay(Settings.RespawnTime, spawnPile, spot)
end

-- Meadow 的每个生成点都有一个金币堆
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
	spawnPile(spot)
end

-- 客户端请求攻击金币堆。绝不能信任客户端:任何人都能触发
-- 这个远程事件,传入任意内容,无论身在何处,都能以任意频率触发。
remotes.Hit.OnServerEvent:Connect(function(player, pile)
	-- 确实存在的金币堆
	if typeof(pile) ~= "Instance" or pile.Parent ~= piles then
		return
	end
	-- 在攻击范围内
	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
	-- 攻击不能太频繁
	local now = time()
	if lastHit[player] and now - lastHit[player] < Settings.HitCooldown then
		return
	end
	lastHit[player] = now
	hitPile(player, pile)
end)

-- 按权重随机抽取:每只宠物的 Chance(以 100 为总数)决定其所占的比例
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

-- 孵化:通过蛋的交互提示触发,花费金币
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()
	-- 如果还有空位,就立即让宠物出场
	local pet = givePet(player, name, equippedCount(player) < Settings.MaxEquipped)
	print(player.Name .. " hatched a " .. name)
end)

-- 出场的宠物会跟随主人:每只宠物在游戏世界中都有一个部件,
-- 每一帧都在玩家身后排成一排
local petModels = {} -- 每只宠物对应它在游戏世界中的部件
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)

Roblox 游戏:宠物模拟器中的所有课

自己练习: Roblox 在线编辑器