Roblox大典
Advertisement

當要監測物件與物件之間碰撞時, 就可以用到

物件.Touched:connect( (function(另外一個物件)
...
end)
或者
function function名字 ( 另一個物件)
...
end

物件.Touched:connect(function名字)

例子

刪除另一個物品 script

例如監測物件b碰到物件a時, 物件b就會自動刪除

  • 當這一個script在物件a內
script.Parent.Touched:connect(function(otherpart)
otherpart:destroy() -- 刪除
end)

Kill script

當某一個玩家碰到這一個 part 時,就會死亡

script.Parent.Touched:connect(function(otherpart)
Humanoid = otherpart.Parent:FindFirstChild("Humanoid") -- 找出碰到的 part 是不是玩家
if Humanoid then
Humanoid.Health = 0 --殺死
end
end)

小遊戲

1. 你現在是路博士鐵路的顧問, 其要求你編寫一個列車安全系統(Fixed Block), 當列車駛經時就會將該路段封閉。

你現在只編寫b區

  • 當列車到達 B1 時, 就會將b區封鎖
  • 當列車完全離開B2時, 就會將b區封鎖
  • 當另外一架列車到達B1時就將其停止(已有function stop(part名字))

列車有以下特微

  • 列車前面有一個part叫 "Front"
  • 列車後面有一個part叫 "Back"
答案
local occupy = false --知道區域是否關閉
script.Parent.B1.Touched:Connect(function(otherpart)
if otherpart.Name == "Front" then
if occupy == false then --檢查是否新列車
occupy = true --關閉該區域
else
stop(otherpart) --停止列車
end
end
end)

script.Parent.B2.Touched:Connect(function(otherpart)
if otherpart.Name == "Back" then
occupy = false
end
end)
Advertisement