Wrapping Custom Player Objects
#
How one might do itA common pattern when using remotes - on the server - is to require casting a player to an object representation of the player.
This may come as the following sort of situation:
- roblox-ts
- luau
As you might have noticed, there's some repeated code where we're repetitively having to grab the player entity here in place of the player.
We can instead use what's called a wrapper to do this for us and make it as if we were connecting directly to the remote and it had the entity as the player argument rather than a player.
#
Using a wrapperWe can define a withPlayerEntity
wrapper, which will achieve what we're doing above in each remote separately. This is the recommended method when augmenting the player argument as it's the safest method and guaranteed to run after all the middleware.
- roblox-ts
- luau
Then we can apply this to the code above.
- roblox-ts
- luau
And as you can see, this reduces repetitive code and gives us the PlayerEntity
object to work with. Simple as that.