【问题标题】:Roblox How to detect a player's teamRoblox 如何检测玩家的团队
【发布时间】:2019-06-26 18:08:57
【问题描述】:

我正在制作一款 Roblox 游戏,我需要以某种方式检测玩家所在的团队。 我的代码目前如下所示:

script.Parent.Touched:Connect(function(part)
    local plr = part.Parent.Name
    if (game.Players:FindFirstChild(plr).Team == "Police") then
        ....
    end
end)

当我触摸那个部分(它是一堵看不见的墙)时,它给了我一个错误:Workspace.Part.Script:3:尝试索引一个 nil 值

我做错了什么?

编辑:我发现它在game.Players中找不到我的名字,因为现在我尝试了:

script.Parent.Touched:Connect(function(hit)
    local plr = game.Players:FindFirstChild(hit.Parent.Name)
    if (plr.Team == "Police") then
...

现在我得到了 Workspace.Part.Script:3:尝试索引本地 'plr'(一个 nil 值)

Edit2:现在我尝试打印 plr (game.Player:FindFirstChild(hit.Parent.Name)),它是“Miniller”,而不是“Miniller”,现在我没有收到任何错误,但是下面的代码也什么都没做。。

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    我通过不使用变量而不是“警察”来解决它,它是 game.Teams.Police,所以代码:

    if (game.Players:FindFirstChild(hit.Parent.Name).Team = game.Teams.Police
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-11
      • 2021-01-13
      • 2016-08-08
      • 2022-10-05
      • 2021-07-14
      • 2020-09-13
      • 1970-01-01
      • 2022-06-21
      相关资源
      最近更新 更多