【发布时间】:2022-01-27 18:44:04
【问题描述】:
所以我在 roblox studio 上制作了一个 CameraScript,当玩家触摸机器人时,相机会聚焦在机器人身上。但是 for 循环似乎不起作用。
game.StarterPlayer.StarterPlayerScripts 中的脚本:
workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
game.Players.LocalPlayer.CharacterAdded:Connect(function(char)
local g = char.Name
print(g) --Just for debugging purposes
print("Player Loaded!")
tou(char)
end)
function tou(char)
print("Function had ran")
for _,p in pairs(char:GetChildren()) do
print("We're here loopin ur parts...")
p.Touched:Connect(function(hit)
print("Someone touched?")
if hit.Parent.Name == "Robot" and hit.Parent:IsA("Model") then
print("It's the robot!")
workspace.CurrentCamera.CFrame = hit.Parent.Look.CFrame
workspace.CurrentCamera.Focus = hit.Parent.Head.CFrame
print("Camlock should be successfull...")
else
print("That ain't a robot tho...")
end
end)
end
end
这是一段不起作用的代码:
for _,p in pairs(char:GetChildren()) do
print("We're here loopin ur parts...")
p.Touched:Connect(function(hit)
print("Someone touched?")
if hit.Parent.Name == "Robot" and hit.Parent:IsA("Model") then
print("It's the robot!")
workspace.CurrentCamera.CFrame = hit.Parent.Look.CFrame
workspace.CurrentCamera.Focus = hit.Parent.Head.CFrame
print("Camlock should be successfull...")
else
print("That ain't a robot tho...")
end
end)
end
我尝试将 for 循环直接放在 CharacterAdded 事件中,将 print() 用于调试,但它只打印了这些:
17:55:24.242 <username> - Client - CamLockOnKill:5
17:55:24.243 Player Loaded! - Client - CamLockOnKill:6
17:55:24.243 Function had ran - Client - CamLockOnKill:12
...但它没有打印其他的。
【问题讨论】:
-
我不熟悉为 Roblox 开发,所以我不知道
GetChildren()究竟做了什么,但唯一的解释是 char 似乎没有任何孩子 -
CharacterAdded 检测玩家是否已加载到世界中,并且第 3 行的 function() 中的 char 参数获取玩家模型,并且事件仅在角色加载时运行(因此得名) ,所以我的角色不可能没有孩子。不过我还是会试试的。
-
我对 Roblox 了解不多,但什么是儿童?为什么你认为 char 至少有一个?你的结果真的应该只是意味着它没有任何孩子
-
这很奇怪,在调用 CharacterAdded 时,您应该在工作区中有一个角色模型。也许这是一个时间问题,尝试在调用
tou之前将wait()放入函数中?也许尝试不同的信号,例如 Player.CharacterAppearanceLoaded?