沙漠之门
Coddy Roblox 游戏:宠物模拟器 课程的第 16 课,共 20 课。
后面的红墙是通往 Desert 的大门,上面有“解锁”提示。支付 Settings.GatePrice 后,大门应该会打开,但只有付款的玩家能通过。
这需要客户端和服务器两端配合。服务器会扣除金币,并将解锁状态记录为玩家的一个属性 Desert,这样每条规则都可以检查它。客户端会监听这个属性,并在自己的屏幕上打开大门:在 Roblox 中,每位玩家的设备都会移动自己的角色,因此,LocalScript 让墙壁变成非实体后,该玩家就能通过,而墙壁对其他玩家仍然是实体。
-- 客户端
workspace.Gate.CanCollide = false
workspace.Gate.Transparency = 0.7用同样的方法隐藏大门的提示,就能避免它再次提供解锁选项。
挑战
困难为付费的玩家打开沙漠大门。
- 在
PetServer中,处理workspace.Gate.ProximityPrompt.Triggered时:如果玩家有Desert属性,或金币少于Settings.GatePrice,就停止;否则扣除费用,并将Desert设为 true。 - 在
PetClient中,openGate():如果me:GetAttribute("Desert"),就将大门的 CanCollide 设为 false,并将 Transparency 设为 0.7。现在调用它,并在 Desert 发生变化时再次调用。
按“开始”,赚取 100 枚金币并解锁大门:墙会渐渐消失。然后点击“检查”。
亲手试试
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)
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)
Roblox 游戏:宠物模拟器中的所有课
自己练习: Roblox 在线编辑器