通过触摸事件移动人形机器人遇到的问题

我正在制作一段过场动画,并且我想通过脚本移动人形机器人,当触摸到某个部位时。

这是我的代码:

game.Workspace.SpawnLocation.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
humanoid:MoveTo(Vector3.new(41.958, 4.264, 66.435))
end)

然而,脚本没有运行。

点赞
用户15138506
用户15138506

马上就能看到,你没有包括一个end来标记脚本的结束。此脚本有时会失败,因此建议您添加FindFirstChild()。

修复的版本:

   workspace.SpawnLocation.Touched:Connect(function(hit)
     local humanoid = hit.Parent:FindFirstChild("Humanoid")

     if not humanoid then
       return
     end

     humanoid:MoveTo(Vector3.new(41.958, 4.264, 66.435))
    end)
2021-02-03 17:42:58