Playersクラス

クラスサービス(game:GetService("Players")で取り出す)Instance.newでは作れない日本語解説あり

ひとことで言うと

ゲームに参加しているプレイヤーを管理するサービスです。プレイヤーが参加したとき・退出したときのイベントを使えます。

公式の説明(英語):A service that contains presently connected Player objects.

使用例

リーダーボード(leaderstats)を作る
Script置く場所:ServerScriptService
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Value = 0
	coins.Parent = leaderstats
end)
参加したプレイヤーの歩く速さを変える
Script置く場所:ServerScriptService
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid") :: Humanoid
		humanoid.WalkSpeed = 32
	end)
end)

スクリプトは、Luauの型チェック(luau-lsp)で確認しています。

クラスの関係

受け継いでいるクラス:Object → Instance → Players

プロパティ(9)

名前と書き方説明
BanningEnabled
Players.BanningEnabled: boolean
Enables or disables the three Players methods (Class.Players:BanAsync()|BanAsync(), Class.Players:UnbanAsync()|UnbanAsync(), and Class.Players:GetBanHistoryAsync()|GetBanHistoryAsync()) that constitute the ban API. This property cannot be set through developer-facing Luau and must be set in Studio through the Players properties window.
BubbleChat
Players.BubbleChat: boolean
Indicates whether or not bubble chat is enabled. It is set with the Class.Players:SetChatStyle() method.
CharacterAutoLoads
Players.CharacterAutoLoads: boolean
Indicates whether Class.Player.Character|characters will respawn automatically.
ClassicChat
Players.ClassicChat: boolean
Indicates whether or not classic chat is enabled; set by the Class.Players:SetChatStyle() method.
LocalPlayer
Players.LocalPlayer: Player
LocalScriptで、自分自身のプレイヤーを表します。サーバー側のScriptではnilです。The Player that the LocalScript is running for.
MaxPlayers
Players.MaxPlayers: int
1つのサーバーに参加できる最大人数。The maximum number of players that can be in a server.
PreferredPlayers
Players.PreferredPlayers: int
The preferred number of players for a server.
RespawnTime
Players.RespawnTime: float
Controls the amount of time taken for a players character to respawn.
UseStrafingAnimations
Players.UseStrafingAnimations: boolean
Determines whether R15 character models play directional strafing and backpedaling animations instead of always turning to face their direction of movement.

メソッド(19)

名前と書き方説明
BanAsync
Players:BanAsync(config: Dictionary): ()
Bans users from your experience, with options to specify duration, reason, whether the ban applies to the entire universe or just the current place, and more. This method is enabled and disabled by the Players.BanningEnabled property, which you can toggle in Studio.
Chat
Players:Chat(message: string): ()
Makes the local player chat the given message.
CreateHumanoidModelFromDescriptionAsync
Players:CreateHumanoidModelFromDescriptionAsync(description: HumanoidDescription, rigType: HumanoidRigType, assetTypeVerification: AssetTypeVerification): Model
Returns a character Model equipped with everything specified in the passed in HumanoidDescription. If HumanoidDescription.UseAvatarSettings is set to true, Avatar Settings in the experience will be applied to the returned model.
CreateHumanoidModelFromUserIdAsync
Players:CreateHumanoidModelFromUserIdAsync(userId: User): Model
Returns a character Model set-up with everything equipped to match the avatar of the user specified by the passed in userId.
GetBanHistoryAsync
Players:GetBanHistoryAsync(userId: User): BanHistoryPages
Retrieves the ban and unban history of any user within the experience's universe. This method is enabled and disabled by the Players.BanningEnabled property, which you can toggle in Studio.
GetCharacterAppearanceInfoAsync
Players:GetCharacterAppearanceInfoAsync(userId: User): Dictionary
Returns information about the character appearance of a given user.
GetFriendsAsync
Players:GetFriendsAsync(userId: User): FriendPages
Returns a FriendPages object which contains information for all of the given player's friends.
GetHumanoidDescriptionFromOutfitIdAsync
Players:GetHumanoidDescriptionFromOutfitIdAsync(outfitId: int64): HumanoidDescription
Returns the HumanoidDescription for a specified outfit, which will be set with the parts/colors/Animations etc of the outfit.
GetHumanoidDescriptionFromUserIdAsync
Players:GetHumanoidDescriptionFromUserIdAsync(userId: User): HumanoidDescription
Returns a HumanoidDescription which specifies everything equipped for the avatar of the user specified by the passed in userId.
GetNameFromUserIdAsync
Players:GetNameFromUserIdAsync(userId: User): string
Sends a query to the Roblox website for the username of an account with a given Class.Player.UserId|UserId.
GetPlayerByUserId
Players:GetPlayerByUserId(userId: User): Player
Returns the Player with the given Class.Player.UserId|UserId if they are in-experience.
GetPlayerFromCharacter
Players:GetPlayerFromCharacter(character: Model): Player
キャラクター(Model)から、そのプレイヤーを調べて返します。ふれたのがプレイヤーかを調べるときによく使います。Returns the Player whose Player.Character matches the given instance, or nil if one cannot be found.
GetPlayers
Players:GetPlayers(): List<Player>
参加しているプレイヤーをすべて配列で返します。Returns a table of all presently connected Player objects.
GetProfileConfigurationFromUserIdAsync
Players:GetProfileConfigurationFromUserIdAsync(userId: User): ProfileConfiguration
Returns the profile configuration of a user as a dictionary.
GetUserIdFromNameAsync
Players:GetUserIdFromNameAsync(userName: string): int64
Sends a query to the Roblox website for the Class.Player.UserId|userId of an account with a given username.
GetUserThumbnailAsync
Players:GetUserThumbnailAsync(userId: User, thumbnailType: ThumbnailType, thumbnailSize: ThumbnailSize): Tuple
Returns the content URL of a player thumbnail given the size and type, as well as a boolean describing if the image is ready to use.
SetChatStyle
Players:SetChatStyle(style: ChatStyle): ()
Sets whether BubbleChat and ClassicChat are being used, and tells TeamChat and Chat what to do.
TeamChat
Players:TeamChat(message: string): ()
Makes the local player chat the given message, which will only be viewable by users on the same team.
UnbanAsync
Players:UnbanAsync(config: Dictionary): ()
Unbans players banned from Class.Players:BanAsync() or the User Restrictions Open Cloud API. This method is enabled and disabled by the Players.BanningEnabled property, which you can toggle in Studio.

イベント(4)

名前と書き方説明
PlayerAdded
Players.PlayerAdded(player: Player)
プレイヤーがゲームに参加したときに発生するイベントです。リーダーボードの準備などに使います。Fires when a player enters the experience.
PlayerMembershipChanged
Players.PlayerMembershipChanged(player: Player)
Fires when the experience server recognizes that a player's membership has changed.
PlayerRemoving
Players.PlayerRemoving(player: Player, reason: PlayerExitReason)
プレイヤーがゲームから退出するときに発生するイベントです。データの保存などに使います。Fires when a player is about to leave the experience.
UserSubscriptionStatusChanged
Players.UserSubscriptionStatusChanged(user: Player, subscriptionId: string)
Fires when the experience server recognizes that the user's status for a certain subscription has changed.

受け継いでいるメンバー

Instance から受け継いでいるもの(46)

Archivable・Capabilities・IsInSandbox・Name・Parent・PredictionMode・Sandboxed・AddTag・ClearAllChildren・Clone・Destroy・FindFirstAncestor・FindFirstAncestorOfClass・FindFirstAncestorWhichIsA・FindFirstChild・FindFirstChildOfClass・FindFirstChildWhichIsA・FindFirstDescendant・GetActor・GetAttribute・GetAttributeChangedSignal・GetAttributes・GetChildren・GetDebugId・GetDescendants・GetFullName・GetStyled・GetStyledPropertyChangedSignal・GetTags・HasTag・IsAncestorOf・IsDescendantOf・IsPropertyModified・QueryDescendants・RemoveTag・ResetPropertyToDefault・SetAttribute・WaitForChild・AncestryChanged・AttributeChanged・ChildAdded・ChildRemoved・DescendantAdded・DescendantRemoving・Destroying・StyledPropertiesChanged

Object から受け継いでいるもの(4)

ClassName・GetPropertyChangedSignal・IsA・Changed

Playersを使う記事

英語の説明とメンバーの一覧は、Roblox Creator Documentation(Players)をもとにしています(CC BY 4.0)。日本語の解説は、このサイトで独自に書いたものです。

執筆:鈴木孝昌官公庁のWeb・システム開発を担当する現役プロジェクトマネージャー。沖縄マイクラ部 クロスウェーブで、ロブロックス(Roblox)・マインクラフト(マイクラ)・Scratchなどのプログラミングを指導。マイクラカップ沖縄代表チームを指導(第6回・第7回、第7回TBS賞)。
沖縄マイクラ部で、子どもたちにロブロックス(Roblox)やマインクラフト(マイクラ)のプログラミングを教えています。プロフィールと編集方針
Instagramで教室の様子を見る
noteで記事を読む