Player - TDMP-Team/TDMP-Lua-Modding-API GitHub Wiki

Getting started

TDMP provides Player API, where you can get all player, get their nicks, positions, and so on.

Usage

To get all connected players, you can use TDMP_GetPlayers(), it will return table of Player Structures, which you can find below

Player Structure

key = description

  • string steamId = SteamID64 of a player
  • string nick = Cached nickname of a player
  • number id = Player's id in TDMP's players table. It's used in TDMP Player API like TDMP_GetPlayerTransform
  • float hp = Current player's hp
  • float health = Same as above
  • string heldItem = Current active tool
  • number veh = Currently driven vehicle (if any)
  • number grabbed = Currently grabbed body (if any)

Globals

These globals are stored values in a Lua environment. Make sure that you're not overriding them because in other way you won't be able to get back the original value.

TDMP_LocalSteamID

string TDMP_LocalSteamID

String containing SteamID of a local player


TDMP_MaxPlayers

number TDMP_MaxPlayers

Number representing maximum amount of player on the server. Currently equals to 8


Functions

These function used internally in Player metatable. If you want to make your life better, then use Player metatable instead, but if you want to get something faster and not willing to create new metatable, then feel free to use these functions

TDMP_GetPlayer

TDMP_GetPlayer(number playerId)

Returns player's structure of certain player

  1. number playerId - Player's id in TDMP's players table

TDMP_GetPlayers

TDMP_GetPlayers()

Returns table of currently connected players.


TDMP_GetPlayerTransform

TDMP_GetPlayerTransform(number playerId)

Returns player's transform

  1. number playerId - Player's id in TDMP's players table

TDMP_GetPlayerCameraTransform

TDMP_GetPlayerCameraTransform(number playerId)

Returns player's camera transform

  1. number playerId - Player's id in TDMP's players table

TDMP_GetPlayerToolTransform

TDMP_GetPlayerToolTransform(number playerId)

Returns player's local tool transform. Note that it doesn't count overrided tool transform

  1. number playerId - Player's id in TDMP's players table

TDMP_IsMe

TDMP_IsMe(number playerId)

Returns whether or not given player is local player

  1. number playerId - Player's id in TDMP's players table

TDMP_IsPlayerInputDown

TDMP_IsPlayerInputDown(number playerId, number buttonId)

Returns whether or not player pressing given button

  1. number playerId - Player's id in TDMP's players table
  2. number buttonId - ID of a button which we need to check. You can include tdmp/tools.lua and use TDMP.Input enum or pass the number itself:
TDMP.Input.mouse1 -- 0
TDMP.Input.lmb -- 0
TDMP.Input.mouse2 -- 1
TDMP.Input.rmb -- 1
TDMP.Input.w -- 2
TDMP.Input.a -- 3
TDMP.Input.s -- 4
TDMP.Input.d -- 5
TDMP.Input.jump -- 6
TDMP.Input.space -- 6
TDMP.Input.crouch -- 7
TDMP.Input.ctrl-- 7

TDMP_IsPlayerInputPressed

TDMP_IsPlayerInputPressed(number playerId, number buttonId)

Returns whether or not player pressed the given button. Currently works only with interact key

  1. number playerId - Player's id in TDMP's players table
  2. number buttonId - ID of a button which we need to check. You can see buttons in TDMP_IsPlayerInputDown above

TDMP_GetPlayerName

TDMP_GetPlayerName(string steamid | number playerId)

Returns nickname of given player. Takes one argument as string steamId of a player or number playerId


Player Metatable

You can use Player metatable for better experience with Player API. To start using it, include this file:

#include "tdmp/player.lua"

Usage

Player

Player(number playerId | string steamId | table playerStructure)

Creates metatable of given player. In argument can be passed player's ID in TDMP table or player's steamId or player's structure table.
Example of usage:

-- Prints everyone's hp in console
#include "tdmp/player.lua"

function tick()
	local players = TDMP_GetPlayers() -- it's good to cache it like that

	for i, ply in ipairs(players) do
		ply = Player(ply)
		-- can also be one of these:
		-- ply = Player(ply.id)
		-- ply = Player(ply.steamId)

		DebugWatch(ply:Nick(), ply:Health())
	end
end

Player:GetTransform

Player:GetTransform()

Returns player's transform as Transform


Player:GetPos

Player:GetPos()

Returns player's position as Vector


Player:GetRotation

Player:GetRotation()

Returns player's rotation as Quat


Player:IsMe

Player:IsMe()

Returns whether or not this player is local player


Player:GetVehicle

Player:GetVehicle()

Returns current player's vehicle handle, if any. Otherwise returns nil


Player:IsDrivingVehicle

Player:IsDrivingVehicle()

Returns whether or not this player is driving vehicle. Equals to

Player:GetVehicle() and Player:GetVehicle() > 0

Player:GetCamera

Player:GetCamera()

Returns player's camera transform as Transform


Player:GetAimDirection

Player:GetAimDirection(Transform camera)

Returns current aim direction of player

  1. Transform camera(optional) - if you already have cached camera's transform of this player, then pass it in here

Player:SteamID

Player:SteamID()

Returns player's SteamID


Player:Nick

Player:Nick()

Returns player's nickname


Player:ID

Player:ID()

Returns player's id in TDMP table


Player:Health

Player:Health()

Returns current player's health in range 0-1


Player:IsDead

Player:IsDead()

Returns whether or not this player is dead. Equals to

Player:Health() <= 0

Player:CurrentTool

Player:CurrentTool()

Returns current active tool of this player


Player:GetToolTransform

Player:GetToolTransform()

Returns local transform of player's active tool


Player:GetToolBody

Player:GetToolBody()

Returns handle of player's active tool's model (if tool exists)


Player:IsInputDown

Player:IsInputDown(number buttonId | string buttonName)

Returns whether or not player is pressing given button

  • number buttonId - ID of a button which you need to check, look in TDMP_IsPlayerInputDown to see what IDs you should pass.
  • OR string buttonName - Name of the button which you need to check, look in TDMP_IsPlayerInputDown to see what strings you should pass. Keep it lower case

Player:IsInputPressed

Player:IsInputPressed(number buttonId | string buttonName)

Returns whether or not player have pressed given button. Currently works only with interact key

  • number buttonId - ID of a button which you need to check, look in TDMP_IsPlayerInputDown to see what IDs you should pass.
  • OR string buttonName - Name of the button which you need to check, look in TDMP_IsPlayerInputDown to see what strings you should pass. Keep it lower case

Player:GetInteractShape

Player:GetInteractShape()

Returns shape handle which player can interact with, same as https://teardowngame.com/modding/api.html#GetPlayerInteractShape


Player:SetVisible

Player:SetVisible(bool state)

Makes player completely invisible (model is hidden, nickname isn't shown), but keeps tool model

  1. bool state - Whether or not player is visible

Player:IsVisible

Player:IsVisible()

Returns whether or not player is currently visible (not hidden)


Player:SetModelVisible

Player:SetModelVisible(bool state)

Makes player's model invisible (by fully removing it), but keeps tool model and still shows player's nickname

  1. bool state - Whether or not player model is visible

Player:IsModelVisible

Player:IsModelVisible()

Returns whether or not player's model is currently visible (not hidden)


Player:SetColor

Player:SetColor(number r, number g, number b)

Sets color to a player. Currently used only by chatbox.
NOTE: Player's color is not synced


Player:GetColor

Player:GetColor(bool asTable)

Returns current player's color. Currently used only by chatbox.
NOTE: Player's color is not synced

  1. bool asTable - if not set or false, function will return 3 args of rgb channels: local r, g, b = ply:GetColor()
    In other case would return a table where each element represents rgb channels:
local col = ply:GetColor(1)
DebugPrint("R: " .. col[1])
DebugPrint("G: " .. col[2])
DebugPrint("B: " .. col[3])