GetPlayers无法在服务器端脚本上工作 [英] GetPlayers not working on server side script

查看:84
本文介绍了GetPlayers无法在服务器端脚本上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Roblox的lua制作游戏.我有从他们的开发人员网站上获得的这段代码示例.

I am learning lua making games in Roblox. I have this sample of code that I got from their developer website.

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

当我将其粘贴到本地脚本中时,此代码有效,但当我将其粘贴到服务器端脚本中时,则无效.我没有收到错误,但没有打印任何内容.我想知道为什么会这样,以及我需要使用什么代码来从服务器端脚本中获取所有播放器.谢谢

This code works when I paste it in a local script but it doesn't when I paste it in a server side script. I don't get an error, but nothing gets printed. I am wondering why this is, and also what code do I need to use to get all players from a server side script. Thanks

编辑--------------------------------------------------

Edit --------------------------------------------------

我还试图在服务器端脚本和本地脚本上运行以下代码:

I have also tried to run this code both on a server side script and a local script:

local players = game.Players:GetChildren()

print(typeof(players))

此代码在本地脚本上运行时,返回:table.在服务器端脚本上运行时,我什么也没得到.这正常吗?

When this code is running on a local script, it comes back with: table. I got nothing when I run it on a server side script. Is this normal?

推荐答案

我相信您遇到计时问题.当您将其作为LocalScript运行时,游戏流程为:

I believe you have a timing issue. When you run this as a LocalScript, the flow of the game is :

  1. 服务器启动,玩家数量= 0
  2. 玩家加入,玩家人数= 1
  3. LocalScript运行-输出所有1个播放器的列表

当您将其作为服务器脚本运行时,游戏流程为:

When you run this as a server Script, the flow of the game is :

  1. 服务器启动,玩家数量= 0
  2. 脚本运行-打印出所有0位玩家的列表

如果您要将示例修改为类似这样的内容:

If you were to modify your example to be something like this:

Players = game:GetService("Players")
print(string.format("Listing all %d player names :", #Players:GetPlayers()))
for i, player in pairs(Players:GetPlayers()) do
    print(i, "- ", player.Name)
end
print("Done listing names")

您应该期望在输出中看到这一点:

You should expect to see this in the output :

Listing all 0 player names :
Done listing names

这篇关于GetPlayers无法在服务器端脚本上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆