Menu
CoddyTech

稀有宠物消息

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

有人孵出传说级宠物时,整个服务器都应该知道。FireAllClients 会同时在每个玩家的客户端上触发 RemoteEvent:

-- 服务器
remotes.Announce:FireAllClients(player.Name, name)
-- 客户端
remotes.Announce.OnClientEvent:Connect(function(who, pet)
end)

每个客户端都会在 HUD 的 Banner 上显示这条消息几秒钟。OnClientEvent 会准确接收服务器发送的内容:客户端这一侧不会额外添加玩家。

challenge icon

挑战

困难

向所有人公告传奇宠物。

  1. 在 PetServer 中,保留 local pet = givePet(...);如果它的 Rarity 是 Legendary,就调用 remotes.Announce:FireAllClients(player.Name, name)。
  2. 在 PetClient 中,remotes.Announce.OnClientEvent 接收 (who, pet):将 hud.Banner.Text 设置为 who .. " hatched a " .. pet .. "!",显示横幅,等待 4 秒后将其隐藏。

按下“开始”并孵化宠物:每 50 个蛋中有 1 个会孵出龙,龙会在每个人的屏幕上显示黄色横幅。然后点击“检查”。

亲手试试

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 desert = spot.Parent.Parent.Name == "Desert"
	local health = desert and Settings.DesertHealth or Settings.PileHealth
	local pile = ServerStorage.CoinPile:Clone()
	pile.Position = spot.Position
	pile:SetAttribute("MaxHealth", health)
	pile:SetAttribute("Health", health)
	pile:SetAttribute("Value", desert and Settings.DesertValue or Settings.PileValue)
	pile:SetAttribute("Zone", spot.Parent.Parent.Name)
	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

-- 每个区域的每个位置都有一个金币堆
for _, zone in workspace.Zones:GetChildren() do
	for _, spot in zone.Spawns:GetChildren() do
		spawnPile(spot)
	end
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
	-- 位于玩家已解锁的区域
	if pile:GetAttribute("Zone") == "Desert" and not player:GetAttribute("Desert") then
		return
	end
	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 在线编辑器