Player class
Part of Flow
Extensions of the Player metatable that the gamemode package adds to every player.
Names go through the GetPlayerName hook so that plugins can replace them,
Player:SetModel announces model changes with the PlayerModelChanged hook, and the
permission checks (Player:can, Player:is_root) are answered by hooks that an admin
plugin implements. The rest covers the initialization state, the data table of a player,
client-side notifications and sounds, freezing, and actions: every player has one current
action, registered with Flux.register_action, which the gamemode runs on each
PlayerThink; Flux.TimedAction builds actions that take time on top of it.
The server-only half adds saving and restoring of the player's database record.
Summary
Methods
Player:give_weapons takes to give everything back.Player:get_weapons_list returns, or its tables with clips, which the weapons are loaded with once they are given.Methods
Checks whether the player has a permission. The decision is made by the 'PlayerHasPermission' hook, which admin plugins implement.
if !actor:can('spawn_props') then
return false
end
Parameters
- action String permission ID
- object Any optional, defaults to
nilobject the permission is checked against, passed to the hook
Returns
- Boolean nil if nothing handles the hook
Cancels the timed action of the player. The callback of the action is called with a failure. Serverside only.
Parameters
- id String optional, defaults to
nilonly cancel the action if it has this ID
Returns
- Boolean true if an action was canceled
Runs the callback of an action registered with Flux.register_action.
Parameters
- id String optional, defaults to
nilaction ID, the current action of the player if omitted
Looks for unobstructed spots around the player that the player can be moved to.
Parameters
- margin Number optional, defaults to
3how far to search: spots are checked on a grid of this many steps in every direction, margin * 10 units apart - filter Entity/List<Entity>/Function optional, defaults to
niltrace filter, the player by default
Returns
- List<Vector> free positions, closest first
Stops the player from firing their active weapon for an hour and freezes the weapon.
See also
Stops the player from moving by setting their move type to MOVETYPE_NONE.
See also
Returns the action the player is currently doing.
Returns
- String action ID, 'none' if the player is not doing anything
Returns the ammo the player has.
Returns
- Map amounts of ammo by ammo type ID, only for the types the player has
Returns the data table of the player. The server keeps the table of every player, while a client is only sent the table of its own local player: for anyone else it gets an empty table there.
Returns
- Map
Returns a value from the data table of the player. Values that were saved during an
earlier session are available once the PlayerRestored hook has run for the player.
Parameters
- key String
- default Any optional, defaults to
nilreturned if the value is not set or is false
Returns
- Any
Returns the timed action the player is doing. On the client only the local player has one.
Returns
- Map the action (see Flux.TimedAction#get), nil if there is none
Returns how far the timed action of the player has progressed. On the client only the local player has a timed action.
Returns
- Number fraction from 0 to 1, nil if the player is not doing a timed action
Returns the weapons the player has: their classes alone, or together with what is
loaded in them and the reserve ammo of the player, in the shape that Player:give_weapons
takes to give everything back.
local weapons, ammo = actor:get_weapons_list(true)
actor:StripWeapons()
actor:StripAmmo()
actor:give_weapons(weapons, ammo)
Parameters
- with_ammo Boolean optional, defaults to
falsetrue to list the clips of the weapons and the reserve ammo of the player as well
Returns
- List<String> weapon classes; when with_ammo is true
- List<Map> tables with the class, clip1 and clip2 fields instead, followed by Map the reserve ammo of the player, a count by ammo type name
Gives several weapons to the player: the classes that Player:get_weapons_list returns,
or its tables with clips, which the weapons are loaded with once they are given. The
weapons come with their default ammo unless the second argument says otherwise.
actor:give_weapons({ 'weapon_pistol', 'weapon_crowbar' })
actor:give_weapons(weapons, true)
actor:give_weapons(weapons, ammo)
Parameters
- weapons_table List<String/Map> weapon classes, or tables with the class, clip1 and clip2 fields as
Player:get_weapons_listlists them with ammo; a clip that is nil is left as the weapon spawns with it - ammo Boolean/Map optional, defaults to
nilwhat reserve ammo comes with the weapons: nil for the default ammo of every weapon, true for none, or counts by ammo type name asPlayer:get_weapons_listreturns them, which are set on the player once the weapons are given and replace what they hold of those types
Checks whether the player has finished loading in and has been initialized by Flux.
Returns
- Boolean
Checks whether the player is currently doing the specified action.
Parameters
- id String action ID
Returns
- Boolean
Checks whether the player has root access, that is can do anything. The decision is made by the 'PlayerIsRoot' hook.
Returns
- Boolean nil if nothing handles the hook
Returns the name of the player. Overrides the engine function, so that plugins can provide their own names (e.g. character names) with the 'GetPlayerName' hook. Also available as Player#name.
Parameters
- force_true_name Boolean optional, defaults to
falseignore the 'GetPlayerName' hook
Returns
- String
On the server
Sends a notification to the player. Serverside variant.
Parameters
- message String text or language phrase
- arguments Map optional, defaults to
nilvalues to substitute into the phrase - color Color optional, defaults to
nil
On the client
Displays a notification to the local player, both as a popup and in the chat. Clientside variant.
Parameters
- message String text or language phrase
- arguments Map optional, defaults to
nilvalues to substitute into the phrase; strings are translated, entities are replaced with their names - color Color optional, defaults to
color_white
Defined in
Sends a light red notification to the player.
Parameters
- message String text or language phrase
- arguments Map optional, defaults to
nilvalues to substitute into the phrase
Plays a gesture on top of the player's regular animations in the custom gesture slot. Called serverside, the gesture is sent to every client through the animation event system, so this is the way to make everyone see a one-off animation such as a punch, a wave or a weapon draw.
Parameters
- animation Number activity (ACT_ enum) or String sequence name
Returns
- Boolean false if the player's model has no such sequence
On the server
Plays a sound once on the client of the player, at full volume and without a position. Serverside variant.
Parameters
- path String path of the sound file, relative to the sound/ folder
See also
On the client
Plays a sound once for the local player, at full volume and without a position. Clientside variant: does nothing when called on another player.
Parameters
- path String path of the sound file, relative to the sound/ folder
Defined in
Resets the action of the player to 'none'.
Loads the database record of the player by their SteamID, creating and saving a new one if they have joined for the first time. The query is asynchronous: the record is put into self.record, the saved data table of the player is loaded from it (values set during this session before the record arrived are kept) and the 'PlayerRestored' hook is run once it has loaded. Nothing happens if the player has left by then. Bots get a blank record that is never saved here.
Checks whether the player is alive and moving on foot faster than the walk speed.
Returns
- Boolean
Saves the database record of the player, together with their data table. Does nothing for bots. Can be prevented by returning true from the 'PreSavePlayerData' hook. Runs the 'PostSavePlayerData' hook afterward. The data table is taken from the server's own copy and never from the networked variables, which are gone once the entity of a leaving player has been removed: without that copy the record keeps the data it already has.
Sets the action the player is currently doing. Does nothing if the player is already doing something, unless forced.
Parameters
- id String action ID
- force Boolean optional, defaults to
falsereplace the current action
Returns
- Boolean true if the action was set, nil otherwise
Player:set_animation(animation, duration_override, callback)
shared packages/flow/lib/sh_animation.lua:515Makes the player play the specified sequence instead of their regular animations. The override is removed once the sequence has finished, and also when the player dies, spawns or gets another model. Called on the server, the animation is shown by every client, including the clients of players who join while it lasts; called on a client, it is only seen on that client. Setting an animation replaces the one that was set before.
target:set_animation('idle_to_sit_ground', nil, function(target, completed)
if completed then
target:set_animation('sit_ground', 0)
end
end)
Parameters
- animation String name of the sequence
- duration_override Number optional, defaults to
nilseconds to keep the override for instead of the duration of the sequence, 0 keeps it until Player#stop_animation is called - callback Function optional, defaults to
nilcalled with the player and a Boolean when the override is removed: true if it was kept for its whole duration, false if it was stopped or replaced before that, which includes the death, the spawn and the model change of the player. Called in the realm the animation was set in
Returns
- Boolean false if the model of the player has no such sequence, in which case nothing changes and the callback is not called
Replaces the data table of the player. The table is networked to the player it belongs to, and to nobody else, and written to the database record of the player, which stores it the next time the record is saved.
Parameters
- data Map optional, defaults to
{}values thattable.serializecan store
Sets whether the player has been initialized by Flux.
Parameters
- initialized Boolean optional, defaults to
true
Sets a value in the data table of the player. The value is networked to the player it
belongs to, and to nobody else, and persistent: it is saved with the database record of
the player (when they disconnect, when their character is saved and when the server
shuts down) and is there again the next time they join. Keep the values to what
table.serialize can store.
target:set_player_data('tutorial_seen', true)
Parameters
- key String
- value Any nil removes the value
Sets the model of the player. Overrides the engine function to run the 'PlayerModelChanged' hook and, on the server, to tell the clients about the change.
Parameters
- path String path to the model
On the server
Starts a named looping sound on the client of the player. Serverside variant.
Parameters
- id String name to stop the sound by
- path String path of a looping sound file, relative to the sound/ folder
- volume Number optional, defaults to
0.75volume from 0 to 1
See also
On the client
Starts a named looping sound for the local player. Starting another sound under a name that is in use replaces the old one, and starting the same sound again while it plays does nothing. Clientside variant: does nothing when called on another player.
Parameters
- id String name to stop the sound by
- path String path of a looping sound file, relative to the sound/ folder
- volume Number optional, defaults to
0.75volume from 0 to 1
Defined in
Starts a timed action for the player. Serverside only.
actor:start_timed_action('search', 3, {
text = 'ui.hud.bar_text.search',
condition = Flux.TimedAction:looking_at(container),
callback = function(actor, success)
if success then
open_container(actor, container)
end
end
})
Parameters
- id String action ID, which becomes the current action of the player
- duration Number seconds the action takes
- options Map optional, defaults to
niltext, arguments, condition, callback and force, see Flux.TimedAction#start
Returns
- Boolean true if the action has started
See also
Returns the Steam name of the player.
Returns
- String
Stops the animation that was set with Player#set_animation. Called on the server, it stops on every client as well. The finish callback of the animation is called with false.
On the server
Stops a named sound on the client of the player. Serverside variant.
Parameters
- id String name the sound was started under
- fade_out Number optional, defaults to
0seconds over which the sound fades out
See also
On the client
Stops a sound that was started with Player#start_sound. Clientside variant: does nothing when called on another player.
Parameters
- id String name the sound was started under
- fade_out Number optional, defaults to
0seconds over which the sound fades out; 0 cuts it off
Defined in
Lets the player fire their active weapon again and unfreezes the weapon.
See also
Lets the player move again by setting their move type to MOVETYPE_WALK.
See also
Moves the player to the closest unobstructed spot nearby.
Parameters
- filter Entity/List<Entity>/Function optional, defaults to
niltrace filter, the player by default