沙漠金币
Coddy Roblox 游戏:宠物模拟器 课程的第 17 课,共 20 课。
Desert 有自己的地点,而且那里的堆更坚固、价值更高:Settings.DesertHealth 和 Settings.DesertValue。在每个区域生成堆,并给每个堆添加一个 Zone 属性,说明它所在的位置。地点知道自己所属的区域:它位于 Zones.Desert.Spawns 中,所以 spot.Parent.Parent.Name 的值是 "Desert"。
闸门只会在玩家自己的屏幕上打开,利用漏洞的玩家可以在自己的屏幕上打开任意墙壁。因此,服务器的 Hit 检查要再增加一条规则:没有 Desert 属性的玩家不能拾取 Desert 堆。客户端负责打开门,服务器负责维护规则。
挑战
困难让沙漠里布满更坚固、价值更高的堆。
- 在
spawnPile中,如果某个地点的spot.Parent.Parent.Name是Desert,就使用Settings.DesertHealth和Settings.DesertValue;将每个堆的Zone属性设为该名称。 - 在
workspace.Zones中每个区域的地点上生成堆。 - 在
Hit处理程序中,如果玩家没有设置Desert属性,就忽略沙漠中的堆。
按下“开始”:沙地上会出现标记为 8/8 的堆。然后按下“检查”。
亲手试试
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
-- 第 17 课:Desert Coins 的代码写在这里
-- 在 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)
elseif model then
-- 收起宠物:移除它的部件
model:Destroy()
petModels[pet] = nil
end
end
end
end)
-- 客户端请求放出或收起宠物。服务器会检查:这只宠物
-- 是否属于该玩家,以及放出的宠物是否不超过 MaxEquipped 只。
remotes.Equip.OnServerEvent:Connect(function(player, pet)
if typeof(pet) ~= "Instance" or pet.Parent ~= player.Pets then
return
end
if pet:GetAttribute("Equipped") then
pet:SetAttribute("Equipped", false)
elseif equippedCount(player) < Settings.MaxEquipped then
pet:SetAttribute("Equipped", true)
end
end)
-- Desert 的大门:付费一次即可打开,但只对该玩家开放
workspace.Gate.ProximityPrompt.Triggered:Connect(function(player)
local coins = player.leaderstats.Coins
if player:GetAttribute("Desert") or coins.Value < Settings.GatePrice then
return
end
coins.Value -= Settings.GatePrice
player:SetAttribute("Desert", true)
end)
Roblox 游戏:宠物模拟器中的所有课
自己练习: Roblox 在线编辑器