Menu
CoddyTech

硬币堆

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

金币来自堆。Meadow 有 8 个放置点,它们是 workspace.Zones.Meadow.Spawns 中看不见的部件,而 ServerStorage.CoinPile 就是堆本身,它会待在玩家看不到的地方。

要把一堆金币放到某个点位上,先克隆模板,再把副本移到那里。每堆金币还需要记录自己的生命值和价值。属性可以将这类值保存在实例本身上:

pile:SetAttribute("Health", 3)
print(pile:GetAttribute("Health")) --> 3

每个副本都有自己的属性,所以每堆金币都有自己的生命值。

challenge icon

挑战

简单

在 Meadow 的每个位置放一堆金币。

  1. 在靠近顶部的位置添加:local ServerStorage = game:GetService("ServerStorage") 和 local piles = workspace.Piles。
  2. 编写 spawnPile(spot):将 ServerStorage.CoinPile 克隆到该位置的 Position;将属性 MaxHealth 和 Health 设为 Settings.PileHealth,将 Value 设为 Settings.PileValue;将它的父级设为 piles;然后返回它。
  3. 对 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 在线编辑器