HasElementDataSubscriber: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Remove obsolete Requirements section)
 
(2 intermediate revisions by 2 users not shown)
Line 23: Line 23:
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
<syntaxhighlight>
<syntaxhighlight>
TODO
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
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
==Requirements==
{{Requirements|1.5.7-9.20477|n/a|}}


==See Also==
==See Also==
{{Element_functions}}
{{Element_functions|server}}

Latest revision as of 17:19, 7 November 2024

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

See Also