2016-12-31 576 views
1

我試圖在我的ROBLOX地方製造一個UFO,並且想要創建一個當UFO通過頭頂時會播放音頻的系統。我創建了一個零件,在零件中插入了一個音頻,然後在零件內放置了一個腳本。所以它看起來像:如何在Lua中使用多個If?

本部>音頻 - >腳本

我計劃的系統時,一個人形觸摸註冊,而如果部分會比快說,每秒300個道釘,我想它播放音頻(最好的音頻將僅適用於由部分觸及人飾演)所以我寫了一個腳本,去如下:

while true do 
if script.parent.parent.Velocity.Magnitude>299 then 
    script.Parent:play() 
    wait(5) 
    script.Parent:stop() 
else 
    wait() 
end 
wait() 

,你可以看到我錯過了關於被觸及的人形部分,但我不知道如何記住那個。我對腳本非常陌生,但我不知道這些命令(?)的正確上下文。幫助將不勝感激。

謝謝!

回答

1

您可以使用Lua的邏輯運算符:'和','或'和'不是'是最常用的。對你來說,這聽起來像你想要做的事,如:

if (condition1) and (condition2) then 
    play_sound() 
else 
    wait() 
end 

您也可以在「鳥巢」的if語句:

if condition1 then 
    if condition2 then 
     do_something() 
    end 
end 
+0

所以我的腳本到目前爲止是: – Marcus

0

除了@的會回答你也可以使用elseif檢查不同的條件。

if (conditionA) then 
elseif (conditionB) then 
end 

如果conditionA爲true,則不會檢查下一個語句,因此順序非常重要。

while true do 
if script.parent.parent.Velocity.Magnitude>299 then 
if(humanoidIsTouchedd()) then 
    script.Parent:play() 
    wait(5) 
    script.Parent:stop() 
elseif (somethingElseIsTouched()) 
    doSomethingElse() 
else 
    wait() 
end 
wait()