服务器端脚本中的getPlayers无法正常工作?

为什么这段代码在本地脚本中可以工作,但在服务器端脚本中却不行?

local Players = game:GetService("Players")

for i, player in pairs(Players:GetPlayers()) do
    print(player.Name)
end
点赞
用户9627858
用户9627858

这是一个服务器脚本,因此它在游戏启动时运行。(在玩家加入之前)

你可能在寻找 PlayerAdded 事件:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    print(player.Name)
end)

这个脚本可以在本地运行,因为当玩家加入游戏时,本地脚本会运行,所以会有一个玩家可以找到。

或者可以把它放在循环中,你可以使用当前脚本,你只需要把它放到循环中:

local Players = game:GetService("Players")

while true do
    wait(howLongBetween)
    for i, player in pairs(Players:GetPlayers()) do
        print(player.Name)
    end
end
2020-01-29 22:25:16
用户12839543
用户12839543

代码在游戏中没有任何玩家之前就执行。考虑添加 wait()。

2020-02-04 14:37:21