最多装备三只宠物
Coddy Roblox 游戏:宠物模拟器 课程的第 15 课,共 20 课。
按下宠物的按钮后,应将它放出或收回。客户端上的 TextButton 在被按下(单击或轻触)时会触发 Activated。客户端通过 remotes.Equip 向服务器发送请求,并发送宠物本身。
服务器会像往常一样进行检查:宠物必须是 当前 玩家 Pets 中的一个 Instance(利用漏洞的人可能会发送任何内容),而且放出的宠物不能超过 Settings.MaxEquipped 只。然后服务器会切换 Equipped,更改会同步回来:列表会重新绘制,宠物也会出现或消失。
挑战
中等用对应的按钮让宠物出战或收回。
- 在
PetClient中,每个按钮的Activated都会触发remotes.Equip,并传入对应的宠物。 - 在
PetServer中,处理remotes.Equip.OnServerEvent时:如果宠物的 Parent 不是player.Pets,就停止处理;切换Equipped,只有当前出战宠物少于Settings.MaxEquipped时,才让一只宠物出战。 - 在 Heartbeat 中,销毁已收回宠物的部件。
按“开始”,然后按 Cat(出战),Cat 就会被收回。然后按“检查”。
亲手试试
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)
-- 第 15 课:在这里添加最多装备三只宠物的代码
这节课包含一个小测验。开始学习即可作答,并记录你的进度。
Roblox 游戏:宠物模拟器中的所有课
自己练习: Roblox 在线编辑器