硬币堆
Coddy Roblox 游戏:宠物模拟器 课程的第 2 课,共 20 课。
金币来自堆。Meadow 有 8 个放置点,它们是 workspace.Zones.Meadow.Spawns 中看不见的部件,而 ServerStorage.CoinPile 就是堆本身,它会待在玩家看不到的地方。
要把一堆金币放到某个点位上,先克隆模板,再把副本移到那里。每堆金币还需要记录自己的生命值和价值。属性可以将这类值保存在实例本身上:
pile:SetAttribute("Health", 3)
print(pile:GetAttribute("Health")) --> 3每个副本都有自己的属性,所以每堆金币都有自己的生命值。
挑战
简单在 Meadow 的每个位置放一堆金币。
- 在靠近顶部的位置添加:
local ServerStorage = game:GetService("ServerStorage")和local piles = workspace.Piles。 - 编写
spawnPile(spot):将ServerStorage.CoinPile克隆到该位置的 Position;将属性MaxHealth和Health设为Settings.PileHealth,将Value设为Settings.PileValue;将它的父级设为piles;然后返回它。 - 对
workspace.Zones.Meadow.Spawns中的每个位置调用这个函数。
按“开始”:草地上就会出现一堆堆金币。然后按“检查”。
亲手试试
local Players = game:GetService("Players")
local Settings = require(game.ReplicatedStorage.Settings)
print("A Meadow pile gives " .. Settings.PileValue .. " coins")
-- 每位玩家的排行榜上都有 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
end)
-- 第 2 课:Coin Piles 的代码写在这里
Roblox 游戏:宠物模拟器中的所有课
自己练习: Roblox 在线编辑器