评估多个声明

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

如果声明可以有多个需要在运行代验证码之前满足的要求。关键字 and 允许您组合声明。以下代码首先评估两加两等于六,然后如果四不等于六。如果两个声明都是真的,代码将奔跑。


-- 不会运奔跑
if 2 + 2 == 6 and 4 ~= 6 then
print("Both statements are true")
end
-- 将运奔跑
if 4 + 2 == 6 and 4 ~= 6 then
print("Both statements are true")
end

创建一个增强功能

强化是经验中的物品,可以为玩家提供飞行、隐形或速度等特殊能力。这个强化将每次触碰强化时提高玩家的步行速度。持续应用提升可能会让玩家前进太快,因此 and 将用于控制上限的最高步行速度。

设置增强功能

使用此代码与简单的部件或模型,例如水晶、硬币或发光的霓虹球。

  1. 创建一个名为 增强 的新部分,并插入一个名为 WalkSpeedManager 的脚本。

  2. 宣言一个名为 speedBoost 的变量并分配脚本的父对象。


    -- 触摸时提供暂时速度提升
    local speedBoost = script.Parent
  3. 设置一个名为 onTouch 的函数并将其连接到父对象的 Touched 事件。然后进行测试并检查你的工作。


    local speedBoost = script.Parent
    local function onTouch(otherPart)
    print("Something touched speedBoost")
    end
    speedBoost.Touched:Connect(onTouch)
  4. 步行速度属性发现在 Humanoid 对象上。使用创建陷阱部分时使用的相同模式,并创建一个条件,检查人形对象。

    寻找人形部件

    local function onTouch(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
    print("A Humanoid was found")
    end
    end

加快玩家速度

速度提升将使虚拟形象每次触发速度提升时走得更快。这将很快变得非常、非常快。关键字 and 将确保玩家不能以过快的速度前进,只有在玩家处于特定速度下启用速度提升。

  1. 如果找到了一个人形怪物,请将当前的步行速度值加入 10 然后使用。游戏测试,你的虚拟形象每次触碰速度提升都会变得更快

    提高当前的步行速度

    local function onTouch(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
    humanoid.WalkSpeed += 10
    end
    end
    speedBoost.Touched:Connect(onTouch)
  2. 在 if 声明中,使用关键字 and 添加第二个条件,即当前的 WalkSpeed 值小于 50。添加强化后,最快的步行速度值将为 60。

    检查当前步行速度是否为 50 或更少

    if humanoid and humanoid.WalkSpeed <= 50 then
    humanoid.WalkSpeed += 10
    end

精确调整速度提升

触摸时每次触摸速度提升都会被调用。每一步或最轻微的反弹都会触发触碰事件并调用连接的函数。零件的属性, CanTouch 可以防止触发触碰事件。利用 CanTouch 并每次激活时关闭速度提升一秒钟。

  1. 在应用提升后,将零件的 CanTouch 属性设置为 false。进行游戏测试并确保提升仅适用一次。

    禁用速度提升

    local speedBoost = script.Parent
    local function onTouch(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    if humanoid and humanoid.WalkSpeed <= 50 then
    humanoid.WalkSpeed += 10
    speedBoost.CanTouch = false
    end
    end
    speedBoost.Touched:Connect(onTouch)
  2. 使用 task.wait(1) 暂停脚本一秒钟,然后将 CanTouch 设置为真实。进行游戏测试,确保在一秒后可以重新应用速度提升。

    完成的脚本

    local speedBoost = script.Parent
    local function onTouch(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    if humanoid and humanoid.WalkSpeed <= 50 then
    humanoid.WalkSpeed += 10
    speedBoost.CanTouch = false
    task.wait(1)
    speedBoost.CanTouch = true
    end
    end
    speedBoost.Touched:Connect(onTouch)
  3. 与完成的脚本中的值进行游戏。走路速度最高可达 100。默认走路速度值为 16。

摘要

关键字 and 可用于在运行代码块之前要求多个条件,例如值大于 0 且小于 100。或者如果存在一个人形怪物,其行走速度小于或等于 50。