【问题标题】:roblox studio plr.Name == ""roblox studio plr.Name == ""
【发布时间】:2019-07-24 06:46:04
【问题描述】:

我正在制作一个脚本来检测一个使用他们的名字的人,但我似乎无法让它工作。还请指出我在脚本中犯的任何错误,这会很有帮助。

game.Workspace:WaitForChild("Console")
print("Waited")
game.Players.PlayerAdded:Connect(function(plr)
    print("Connected")
    if game.Workspace.Console and plr.Name == "wojciechpa2007" then
        local Console = game.Lighting.Console:Clone()
        Console.Parent = plr.Startergui
        print("Cloned")
    elseif
        not game.Workspace.Console and plr.Name == "wojciechpa2007" then
            plr.Startergui.Console:Destroy()
            print("Destroyed")
    end
end)

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    嘿嘿,

    这个脚本有一个竞态条件。您的第一行 game.Workspace:WaitForChild("Console") 将阻止脚本其余部分的执行,直到加载对象或达到超时。

    这意味着玩家有可能在脚本侦听game.Players.PlayerAdded 信号之前加入游戏。

    另外StarterGui 不存在于特定播放器上。它存在于游戏级别,是一个桶,当玩家的角色加载到游戏中时,它会将其内容转储到玩家的PlayerGui

    所以要修复你的脚本,你可以尝试这样的事情:

    -- right away connect to the PlayerAdded signal
    game.Players.PlayerAdded:Connect(function(plr)
        print("Player Joined!", plr.Name, plr.UserId)
    
        -- do something special if wojciechpa2007 joins
        if plr.Name == "wojciechpa2007" then
            print("wojciechpa2007 joined! Adding console!")
    
            -- add the special console into the player's PlayerGui for when they load
            local Console = game.Lighting.Console:Clone()
            Console.Parent = plr.PlayerGui
         end
    end)
    

    这里有一些建议和注意事项:

    • 查看玩家的UserId 比查看他们的名字更安全。 Roblox 允许您更改姓名,但您的 UserId 始终相同。
    • 在您的 StarterGui 中添加一些内容会使其在下次加载角色时显示在您的 PlayerGui 中。但如果您的角色已经加载完毕,您将在下次重生时才能看到它。
    • 如果您的 Console 对象是某种 GUI 元素,请确保在将其插入播放器之前,它是 ScreenGui 对象的父级。否则它就不会显示。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2021-06-30
      • 2020-09-01
      • 1970-01-01
      • 2020-10-25
      • 2021-11-12
      • 2019-03-05
      • 2020-04-13
      • 2019-03-24
      • 2019-11-22
      相关资源
      最近更新 更多