在基础脚本教程期间,您已经编写了单个部分来创建可播放的场景。使用以前的方法,如果你复制了零件,那么你也会有复制的脚本。这将使更新脚本变得枯燥,因为更改必须通过脚本进行。
在本教程中,将使用不同的模式来创建多个健康提取,只有一个复制本脚本确定健康提取行为的脚本。当拾取被触碰时,它将恢复玩家的生命值,稍微消失并在短时间内被禁用。
设置
首先,你需要一个部件或模型来作为拾取使用。对决镇示例世界包含大量分布在地图上的生命拾取。

每次生命拾取都是由两个长方形零件组成的联盟,内部包含绿色点光源。它们都存储在工作区中的一个文件夹中,名为 HealthPickups ,这是脚本寻找它们的地方。如果您向地图添加更多内容,您必须确保它们也存储在此文件夹中。


恢复健康
首先,脚本需要恢复玩家的生命值。这个模式应该在 致命熔岩 教程中对你熟悉。
在 服务器脚本服务 中,添加一个名为 PickupManager 的脚本。
在这个脚本中,宣言一个名为 MAX_HEALTH 的常量,其值为 100 。
创建一个名为 onTouchHealthPickup 的函数,其参数为另一部分触发了拾取和拾取本身。
local MAX_HEALTH = 100local function onTouchHealthPickup(otherPart, healthPickup)end在函数中,从 otherPart 的父获得角色模型。接下来,检查是否使用了 Humanoid 来执行 FindFirstChildWhichIsA() 。
如果它有一个人形怪物,将其 生命值 属性设置为 MAX_HEALTH。
local MAX_HEALTH = 100local function onTouchHealthPickup(otherPart, healthPickup)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid thenhumanoid.Health = MAX_HEALTHendend
获取选取文件夹
保持健康拾取的文件夹可能在脚本运行时没有加载到游戏。WaitForChild 可用于暂停脚本并在加载时获取 HealthPickups 文件夹。
当调用文件夹时,GetChildren 函数将返回文件夹内容的阵列。
在 MAX_HEALTH 下,宣言一个名为 healthPickupsFolder 的变量,并使用 WaitForChild 函数从工作区获得 HealthPickups 文工作间夹。
创建一个名为 healthPickups 的变量来存储调用 GetChildren 函数的结果在 healthPickupsFolder 上。
local MAX_HEALTH = 100local healthPickupsFolder = workspace:WaitForChild("HealthPickups")local healthPickups = healthPickupsFolder:GetChildren()local function onTouchHealthPickup(otherPart, healthPickup)local character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid thenhumanoid.Health = MAX_HEALTHendend
与 ipairs 循环
onTouchHealthPickup 需要在阵数组中的每次健康提取中调用。为了高效地执行此操作,将使用新的循环语法。
ipairs 是一个可以用 for 循环来通过阵数组的每个元素的函数。你不需要指定循环的开始和结束位置。使用 ipairs 的循环 A 被定义如下:
- 索引 :这与普通循环中的控制变量相等。
- 值 :这将由循环遍历阵列中的每个元素填充,直到循环结束。命名变量值后,应该与它实际上包含的内容相关。
- 阵列 : 你想循环的阵列被传递到 ipairs 函数。
在以下验证码中,你不需要为任何东西索引,因此可以使用 _ 将其留空。使用 **** 函数创建一个循环 ipairs ,传递 healthPickups .
local function onTouchHealthPickup(otherPart, healthPickup)
local character = otherPart.Parent
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid.Health = MAX_HEALTH
end
end
for _, healthPickup in ipairs(healthPickups) do
end
需要包装函数来将健康检测传递到 onTouchHealthPickup 函数,当连接到 Touched 事件时。
在 for 循环中,将触发事件连接到一个匿名函数,其参数名为 otherPart 。
调用 onTouchHealthPickups 函数,传递 both the otherPart 参数和 the healthPickup .
for _, healthPickup in ipairs(healthPickups) dohealthPickup.Touched:Connect(function(otherPart)onTouchHealthPickup(otherPart, healthPickup)end)end
现在测试你的代码,你应该发现健康提取恢复了你的健康。你需要先伤害你的玩家 - 尝试站在生成点旁边的通风口上。
生命条应该出现在右上角,当玩家被治疗时会消失。
接收冷却时间
目前,拾取将永久治愈任何触碰它的玩家。在游戏中,如果只能捡起一次,在使用前有短时间冷却期,那将更有效。
首先,您需要记录是否在冷却期内拾取。下面的模式应该从 褪色陷阱 中熟悉 - 这一次,缓冲将通过在健康拾取上设置一个属性来实现。
在 for 循环中,设置新的 属性 称为 "Enabled" 到 true 。
将代码包含在 onTouchHealthPickup 中的 if 声明中,条件为 healthPickup:GetAttribute("Enabled") 。
local function onTouchHealthPickup(otherPart, healthPickup)if healthPickup:GetAttribute("Enabled") thenlocal character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid thenhumanoid.Health = MAX_HEALTHendendendfor _, healthPickup in ipairs(healthPickups) dohealthPickup:SetAttribute("Enabled", true)healthPickup.Touched:Connect(function(otherPart)onTouchHealthPickup(otherPart, healthPickup)end)end
禁用拾取
拾取应提供视觉反馈称它已禁用 - 一个常见的方法是让它略透明。
在脚本顶部宣言三个常量(随意调整每个值以满足需求):
- ENABLED_TRANSPARENCY = 0.4
- DISABLED_TRANSPARENCY = 0.9
- COOLDOWN = 10
local MAX_HEALTH = 100local ENABLED_TRANSPARENCY = 0.4local DISABLED_TRANSPARENCY = 0.9local COOLDOWN = 10local healthPickupsFolder = workspace:WaitForChild("HealthPickups")在 onTouchHealthPickup 中的 if 声明中,将拾取的 Transparency 设置为 DISABLED_TRANSPARENCY,并将 Enabled 属性的值设置为 false。
local function onTouchHealthPickup(otherPart, healthPickup)if healthPickup:GetAttribute("Enabled") thenlocal character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid thenhumanoid.Health = MAX_HEALTHhealthPickup.Transparency = DISABLED_TRANSPARENCYhealthPickup:SetAttribute("Enabled", false)endendend调用 task.wait() 函数,将 COOLDOWN 传递为要等待的数量。
将 Transparency 设置为 ENABLED_TRANSPARENCY 并将 Enabled 设置为 true 。
local function onTouchHealthPickup(otherPart, healthPickup)if healthPickup:GetAttribute("Enabled") thenlocal character = otherPart.Parentlocal humanoid = character:FindFirstChildWhichIsA("Humanoid")if humanoid thenhumanoid.Health = MAX_HEALTHhealthPickup.Transparency = DISABLED_TRANSPARENCYhealthPickup:SetAttribute("Enabled", false)task.wait(COOLDOWN)healthPickup.Transparency = ENABLED_TRANSPARENCYhealthPickup:SetAttribute("Enabled", true)endendend
再次测试您的选择:您应该发现当您触摸选择时,它会恢复您的健康,变得透明,然后回来准备再次使用。
如果您想使回馈对玩家在拾取时更具影响力,请尝试在更改透明度时将点光源的亮度切割。
尝试在自己的项目中使用这些健康提取物,或更改外观和效果以给予玩家不同类型的强化。
最终代验证码
local MAX_HEALTH = 100
local ENABLED_TRANSPARENCY = 0.4
local DISABLED_TRANSPARENCY = 0.9
local COOLDOWN = 10
local healthPickupsFolder = workspace:WaitForChild("HealthPickups")
local healthPickups = healthPickupsFolder:GetChildren()
local function onTouchHealthPickup(otherPart, healthPickup)
if healthPickup:GetAttribute("Enabled") then
local character = otherPart.Parent
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
humanoid.Health = MAX_HEALTH
healthPickup.Transparency = DISABLED_TRANSPARENCY
healthPickup:SetAttribute("Enabled", false)
task.wait(COOLDOWN)
healthPickup.Transparency = ENABLED_TRANSPARENCY
healthPickup:SetAttribute("Enabled", true)
end
end
end
for _, healthPickup in ipairs(healthPickups) do
healthPickup:SetAttribute("Enabled", true)
healthPickup.Touched:Connect(function(otherPart)
onTouchHealthPickup(otherPart, healthPickup)
end)
end