roblox studio plr.Name ==“”

问题描述 投票:0回答:1

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

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个回答
1
投票

heyo,

此脚本中包含竞争条件。第一行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对象的父级。否则它就不会出现。

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.