Menu
CoddyTech

熔岩地板

本课属于 Coddy Lua 学习旅程的“Roblox 游戏脚本编写”单元。第 12 课,共 25 课。

通过触摸垫,你知道 hit 是身体部位。它的 Parent 是角色,也就是包含所有身体部位的模型。角色还包含一个 Humanoid,它拥有 Health。

FindFirstChild 会按名称查找子对象,如果没有找到,就返回 nil(空值):

local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
	print(humanoid.Health)
end

当 humanoid 是 nil 时,if 会跳过代码块。掉落的球没有 Humanoid,如果把 nil 当作 Humanoid 来使用,那么每次有非玩家对象碰到这个部件时,Output 中都会报错。

当 Humanoid 的 Health 达到 0 时,它就会被击败,玩家会在几秒后重生。

challenge icon

挑战

中等

把发光的橙色条变成熔岩。

  1. 在 Touched 函数中,使用 FindFirstChild 在 hit.Parent 中查找 Humanoid。
  2. 如果找到了,就把它的 Health 设为 0。“检查”会在熔岩上放下一个球:它没有 Humanoid,因此不能导致错误。

按“开始”,然后走进熔岩:你会被击败,并在 5 秒后回来。然后按“检查”。

亲手试试

local lava = script.Parent

lava.Touched:Connect(function(hit)

end)

Roblox 游戏脚本编写中的所有课

自己练习: Lua 在线编译器