Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
For loop GetPlayer不使用服务器端脚本?_For Loop_Lua_Server Side_Roblox - Fatal编程技术网

For loop GetPlayer不使用服务器端脚本?

For loop GetPlayer不使用服务器端脚本?,for-loop,lua,server-side,roblox,For Loop,Lua,Server Side,Roblox,为什么这段代码可以在本地脚本上工作,而不能在服务器端脚本上工作 local Players = game:GetService("Players") for i, player in pairs(Players:GetPlayers()) do print(player.Name) end 这是一个服务器脚本,所以它在游戏开始时运行。(在玩家加入之前) 您可能要查找的是PlayerAdded事件: local Players = game:GetService("Players")

为什么这段代码可以在本地脚本上工作,而不能在服务器端脚本上工作

local Players = game:GetService("Players")

for i, player in pairs(Players:GetPlayers()) do
    print(player.Name)
end

这是一个服务器脚本,所以它在游戏开始时运行。(在玩家加入之前) 您可能要查找的是
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

代码在游戏中没有任何玩家之前执行。考虑添加WAITE().< /P>这是否回答了你的问题?