PlayerService - A comfortable way to interact with players

Player Service

Version: public v1


This module is new and might recieve updates in future. If you have any feature suggestions or bug reports, comment on this post.

Features

  • Camel case variable names ( name, displayName )
  • Extra functions ( player:ban(<reason>), player:unban() )
  • More information ( registerDate )
  • Ability to interact with offline players
Getting started

To get a player object, simply run playerService.get(<UserId> | <Player>)

local playerService = require(<Path to module>)

local player = playerService.get(3523014477)

Now, lets interact with it:

print(player.name .. "'s account is " .. player.accountAge .. " days old.")

Well, that would be easier to do with regular player instance, let’s do something more interesting:

print(player.name .. "'s account is " .. player.accountAge .. " days old.\n"
.."Is he banned? " .. tostring(player.banned) .. " Is he ingame? " .. tostring(player.ingame))

ingame property is there for a reason, this module works even if the person is offline! You can interact with players that’s not even ingame, just make sure you remove player object from your scripts to prevent memory leaks.

Now lets try saving & loading user’s data:

if not player.datastore:get('JoinedWhen') then
    player.datastore:set('JoinedWhen', player.accountAge)
end
print(player.name .. ' joined when his account was ' .. player.datastore:get('JoinedWhen') .. ' days old!')

Yeah, its simple as that, you don’t have to pcall or check for errors, this module does that for you automatically, player doesn’t even have to be ingame!

Player online properties:
image
Player offline properties:
image

API Reference

Properties:

name <string>
displayName <string>
id <number>
accountAge <number>
registerDate <number>
banned <boolean>
banReason <any>
ingame <bool>
instance <Player>
character <Model>
CharacterAdded <Signal>

Methods:

player:kick(reason)
player:ban(reason)
player:unban()
player.datastore:get(key)
player.datastore:set(key, value
player:getInfo()

player:getInfo()

Returns:

{
	Devices = {
		['KeyboardEnabled'] = true,
		['GyroscopeEnabled'] = false,
		...
	},
	Focused = true,
	Tick = tick() --user's tick
}

If timed out, kicks


Configurables

Access by module.config

config.DATASTORE_RETRIES - 5
config.DATASTORE_RETRY_COOLDOWN - 1 // Seconds
config.DATASTORE_PREFIX - "PS-"
CLIENT_CALL_TIMEOUT - 5 // Seconds


Roblox Marketplace
File.rbxm (5.5 KB)

8 Likes

I like how other resources always inspire you to make your own great resources, I guess you inspired me to create great resources!

2 Likes

I love the fact that the player doesn’t need to be in-game. It removes the limitation of GetPlayerByUserId

1 Like

This module was made like 5 months ago, I just couldn’t decide when to publish it.

3 Likes

I thought it was like your sarcastic recreations of WHYI_MFAT’s resources where you recreate bad resources

Look at the actual modules first :skull:

1 Like
if runService:IsStudio() and runService:IsServer() and runService:IsClient() then

What is this check supposed to do?
IsServer() and IsClient() return opposite values to each other so this if-statement will never run.

Check out guard clauses, brother. Instead of this:

if self.instance then
 self.instance:Kick(reason)
else
 return warn('Player is not ingame')
end

Do this:

if self.instance then
 return self.instance:Kick(reason)
end

warn('Player is not ingame')

This will reduce cluster in the code, reduce tabulation and will just make it look better overall.
This techinque could be applied to almost every line of your code.

1 Like

All these methods will return true if ran in editor mode, it’s used to prevent creation of RemoteEvent.

1 Like

Changed those functions.

image

You could just use RunService:IsEdit() to be more clear

3 Likes

I read the modules, and thought they were the same [blame my eyes]