因为以下原因,您没有权限编辑本页: 您所请求的操作仅限于这些用户组的用户使用:用户、FANDOM助手、Wiki主管、内容团队人员 您可以查看与复制此页面的源代码。 當要監測物件與物件之間碰撞時, 就可以用到 {| class="article-table" |<syntaxhighlight lang="lua"> 物件.Touched:connect( (function(另外一個物件) ... end) </syntaxhighlight> |- |或者 <syntaxhighlight lang="lua"> function function名字 ( 另一個物件) ... end 物件.Touched:connect(function名字) </syntaxhighlight> |} ==例子== === 刪除另一個物品的Script=== 例如監測物件b碰到物件a時, 物件b就會自動刪除 *當這一個Script在物件a內 {| class="article-table" |<syntaxhighlight lang="lua"> script.Parent.Touched:connect(function(otherpart) otherpart:destroy() -- 刪除 end) </syntaxhighlight> |} ===Kill script=== 當某一個玩家碰到這一個 Part 時,就會死亡 {| class="article-table" |<syntaxhighlight lang="lua"> script.Parent.Touched:connect(function(otherpart) Humanoid = otherpart.Parent:FindFirstChild("Humanoid") -- 找出碰到的 Part 是不是玩家 if Humanoid then Humanoid.Health = 0 --殺死 end end) </syntaxhighlight> |} ==Debounce 不接受== 觸碰Part時會''多次''進行,因此我們需要Debounce保證Script不會重複進行 例: {| class="article-table" |<syntaxhighlight lang="lua"> script.Parent.Touched:connect(function(otherpart) Humanoid = otherpart.Parent:FindFirstChild("Humanoid") -- 找出碰到的 Part 是不是玩家 if Humanoid then Humanoid.Health = Humanoid.Health + 5 --加血 wait(5) -- 以為可以限制加血間隔 end end) </syntaxhighlight> |- |因為 Touch Event 是可以重複進行,因此 wait(5)並不能限制間隔 |} {| class="article-table" |<syntaxhighlight lang="lua"> local debounce = false --不是進行中 script.Parent.Touched:connect(function(otherpart) if debounce == false then --Check Event是不是在進行 debounce = true --Function 進行中 Humanoid = otherpart.Parent:FindFirstChild("Humanoid") -- 找出碰到的 Part 是不是玩家 if Humanoid then Humanoid.Health = Humanoid.Health + 5 --加血 wait(5) -- 限制加血間隔 debounce = false --Function 完結 end end end) </syntaxhighlight> |- |Touch Event 雖然可以重複進行,但會檢查Variable (Debounce)是或否,因此可以限制Event進行次數 |} == 小遊戲== 1. 你現在是路博士鐵路的顧問, 其要求你編寫一個列車安全系統(Fixed Block), 當列車駛經時就會將該路段封閉,防止兩架列車相撞。 {| class="article-table" |[[File:Train.png|thumb|650x650px]] |} 你現在只編寫B區 * 當列車到達 B1 時, 就會將B區封鎖 * 當列車完全離開B2時, 就會將B區解鎖 * 當另外一架列車到達B1時就將其停止(已有function stop(Part名字)), 等候區域再次開放, 使用已有 function start(Part名字)今列車可繼續運行 列車有以下特微 *列車前面有一個Part叫 "Front" *列車後面有一個Part叫 "Back" {| class="article-table" |物件攔 [[File:Instance.png|thumb|200x200px]] |} {| class="mw-collapsible mw-collapsed article-table" ! 答案 |- | <syntaxhighlight lang="lua"> local occupy = false --知道區域是否關閉 script.Parent.B1.Touched:Connect(function(otherpart) if otherpart.Name == "Front" then if occupy == false then --檢查是否新列車 occupy = true --關閉該區域 else stop(otherpart) --停止列車 while occupy do -- 等候區域再次開放 wait() end start(otherpart) occupy = true -- 此列車繼續運行, 區域再次關閉 end end end) script.Parent.B2.Touched:Connect(function(otherpart) if otherpart.Name == "Back" then occupy = false end end) </syntaxhighlight> |} ==學習更多== [[Roblox Studio Scripting 教學]] [https://www.facebook.com/RobloxHKUpdate/ Roblox 更新/教學 Facebook ] [[函數 Function]] 返回至觸碰 Touch Event。