HasElementDataSubscriber

From Multi Theft Auto: Wiki
Jump to navigation Jump to search

This function returns whether a player is subscribed to specific element data. This function is used together with setElementData in "subscribe" mode.

Syntax

bool hasElementDataSubscriber ( element theElement, string key, player thePlayer )

OOP Syntax Help! I don't understand this!

Method: element:hasDataSubscriber(...)


Required Arguments

  • theElement: The element you wish to check whether the player is subscribed to.
  • key: The key you wish to check whether the player is subscribed to.
  • thePlayer: The player you wish to check.

Returns

Returns true if the player is subscribed, false otherwise.

Example

Click to collapse [-]
Server
local nameOfOurElementData = "random" --// name our element data

for i,v in ipairs(getElementsByType("player")) do --// loop through all the players on the server
    setElementData(v, nameOfOurElementData, true, "subscribe") --// set our element data to all players on server
end

function checkIsSubscribed(plr,cmd, key)

    if not key then return end --// check if you've typed element data key

    local randomPlayer = getRandomPlayer() --// getting random player from server

    local isSubscribed = hasElementDataSubscriber(randomPlayer, tostring(key), randomPlayer) --// use our function

    if not isSubscribed then --// if random player is not subscribed to given element data key then add him to subscription

        addElementDataSubscriber(randomPlayer, tostring(key), randomPlayer)
        outputChatBox("Element data key: "..tostring(key).." is now subscribed to: "..getPlayerName(randomPlayer), plr, 255, 255, 255, true)

    else --// if he is subscribed to given element data then remove him from subscription

        removeElementDataSubscriber(randomPlayer, tostring(key), randomPlayer)
        outputChatBox(getPlayerName(randomPlayer).." has been removed from subscription from element data key: "..tostring(key), plr, 255, 255, 255, true)

    end


end
addCommandHandler("checksub", checkIsSubscribed, false, false) --// creating command /checksub not restricted and not CASE sensitive

--// EXAMPLE: /checksub random

Requirements

Minimum server version 1.5.7-9.20477
Minimum client version n/a

Note: Using this feature requires the resource to have the above minimum version declared in the meta.xml <min_mta_version> section. e.g. <min_mta_version server="1.5.7-9.20477" />

See Also