評估多個聲明

*此內容是使用 AI(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. 步行速度屬性發現在人形物體上。使用用於創建陷阱零件時使用的相同模式,並創建一個條件,檢查人形物體。

    尋找人形部件

    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。添加提升後,最快的 WalkSpeed 值將是 60。

    檢查目前的步行速度是否為 50 或少於 50

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

精確調整速度提升

每次速度提升被觸發時,都會呼叫 OnTouch。每一步或輕微彈跳都會觸發觸碰事件並呼叫連接的功能。零件的屬性,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。