宠物会跟着你
Coddy Roblox 游戏:宠物模拟器 课程的第 11 课,共 20 课。
宠物应该跟着你走。孵化出的宠物会立即出现,并带有 Equipped 属性,还会在游戏世界中获得一个部件:它是来自 ServerStorage.PetModels 的模型副本。
服务器会在每一帧把这个部件放在宠物主人的身后。RunService.Heartbeat 每帧触发一次,CFrame 相乘可以将一个位置相对于另一个位置放置:
part.CFrame = root.CFrame * CFrame.new(0, 2, 6) -- 向后 6 格,向上 2 格一个以宠物为键的表会记录对应的部件,因此每只宠物只会获得一个部件,并且只创建一次:
if not petModels[pet] then
petModels[pet] = ServerStorage.PetModels[pet.Value]:Clone()
petModels[pet].Parent = petParts
end挑战
中等孵化出的宠物会跟着你。
- 给
givePet添加第三个参数equipped,并将其存为Equipped属性;孵化时调用givePet(player, name, true)。 - 添加
RunService、local petParts = workspace.PetParts和local petModels = {}。 - 在
RunService.Heartbeat上,为每只已装备的宠物克隆一个ServerStorage.PetModels[pet.Value],放入 petParts(并保存在petModels[pet]中),再将其 CFrame 设置为root.CFrame * CFrame.new(0, -1.5, 4)。
按下“开始”,孵化宠物并走动:你的宠物会跟着你。然后点击“检查”。
亲手试试
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
local remotes = game.ReplicatedStorage.Remotes
local piles = workspace.Piles
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)
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.Parent = player.Pets
return pet
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
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()
givePet(player, name)
print(player.Name .. " hatched a " .. name)
end)
-- 第 11 课:宠物跟随你,代码写在这里
Roblox 游戏:宠物模拟器中的所有课
自己练习: Roblox 在线编辑器