2015-12-20 172 views
1

我需要一點幫助。基本上,我有這樣的代碼:Roblox腳本多次運行?

local plyIsEntered = false 

function onTouched(hit) 

plyIsEntered = true 

if not plyIsEntered then 

end 

if plyIsEntered then 

    local humanoid = hit.Parent:FindFirstChild("Humanoid") 
    local ply = humanoid.Parent 
    if humanoid ~= nil then 

     print("Hit") 
     local playerName = hit.Parent.Name 
     print(playerName) 
     local laserEmitter = game.Workspace["Enterance PC"]:FindFirstChild("laserEmitter") 

     local scanLaser = Instance.new("Part", game.Workspace) 

     scanLaser.Position = laserEmitter.Position 
     scanLaser.Name = "ScanLaser" 
     scanLaser.Size = Vector3.new(1,1,1) 
     local scanLaserMesh = Instance.new("SpecialMesh", game.Workspace.ScanLaser) 
     scanLaserMesh.Name = "Cone mesh" 
     scanLaserMesh.MeshType = "" 
     plyIsEntered = false 


     end 

    end 



end 

script.Parent.Touched:connect(onTouched) 

現在我檢查,如果玩家觸摸一個盒子,它有沒有衝突,是無形的;當他們這樣做時,我想創建一個激光器來掃描它們並打開一扇門。我遇到的問題是當我走進觸發器盒時,它會創建8或9個塊。其中一個塊是我正在應用網格的塊。

我需要做的是確保它只運行一次,而不是創建超過1磚。希望有人能幫助我!

+0

對於宋奇怪的原因,它不會讓我編輯...您需要修改代碼格式化。另外不要忘記,roblox擁有自己的Scripters論壇,他們可以幫助解決這些問題。拋開建議,接觸火警很多,所以你應該看看反彈。 wiki.roblox.com/index.php?title=Debounce – warspyking

回答

1

我相信要解決這個問題,你需要添加一個去抖動。

您會發現,部件的觸摸事件實際上會多次觸發,因此如果代碼位於事件內部,您的代碼將執行多次。

爲了解決這個問題,我們使用去抖動,這意味着如果您的部件在相同的時間範圍內被觸摸太多,您的代碼將無法執行。這裏有一個例子:

local debounce = false 

part.Touched:connect(function() 
    if debounce == false then 
    debounce = true 
    --Your code goes here. 
    wait(1)--Wait one second until you'll be able to execute the code again. 
    debounce = false 
    end 
end) 

要了解更多關於去抖:http://wiki.roblox.com/index.php?title=Debounce