首页 > 解决方案 > 通过触摸事件移动人形机器人的问题

问题描述

我正在做一个过场动画,并且我有兴趣在触摸某个部分时通过脚本移动人形。

这是我的代码:

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

但是脚本没有运行。

标签: luaroblox

解决方案


马上我看到你没有包括结束来标记脚本的结束。这个脚本有时会失败,所以我建议你添加 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)

推荐阅读