minetest.player_exists

From Minetest Developer Wiki
Jump to: navigation, search

Syntax

minetest.player_exists(name)

Description

Checks if a player named name exists in the player database, regardless of whether this player is online or not.

Returns true if the player exists, or false otherwise.

Parameters

  • name - The name of the player.


note.png
Note: Once a player is created and exists in the player database, this method will continue to return true, even if minetest.remove_player(name) was called on that player. You would need to forcefully and fully remove the player from the player backend or reset (delete) the entire player backend altogether (deleting all other players in the process).


Examples

minetest.register_chatcommand("pm", {
    params = "Player, Message",
    description = "Send private message to player",
    func = function(name, param)
        local sendto, message = param:match("^(%S+)%s(.+)$")
 
        -- Check if the player actually exists
        if not minetest.player_exists(sendto) then
            return false, "Player " .. sendto .. " does not exist"
        end
 
        -- Need to loop connected players, as they are ObjectRefs
        for _,player in ipairs(minetest.get_connected_players()) do
            if sendto == player:get_player_name() then
                minetest.chat_send_player(sendto, name .. " sent a PM: " .. message)
                return true, "PM sent to "..sendto
            end
        end
 
        -- Player is not online, send email instead
        send_email(name, sendto, message)
        return true, "Player " .. sendto .. " not online, sent PM as email"
    end,
})
Personal tools
Namespaces

Variants
Actions
Navigation
API
Toolbox