Sign in to follow this  
KamilPL

destroy blip

Recommended Posts

How to destroy blip attached to player who quit ? I always get an error: Bad argument

  
function playerQuit() 
    nick = getPlayerName(source) 
    player = getPlayerFromName(nick) 
    blip = getElementAttachedTo(player) 
    destroyElement(blip) 
end 
addEventHandler("onPlayerQuit", root, playerQuit) 
  

Share this post


Link to post
    function playerQuit() 
        destroy(blip) 
    end 
    addEventHandler("onPlayerQuit", root, playerQuit) 
  

Share this post


Link to post
function playerQuit() 
    local blip =  getBlipAttachedTo(source) 
    if blip then 
        destroyElement(blip) 
    end 
end 
addEventHandler("onPlayerQuit", root, playerQuit) 
  
  
function getBlipAttachedTo( thePlayer ) 
    local blips = getElementsByType( "blip" ) 
    for k, theBlip in ipairs( blips ) do 
        if getElementAttachedTo( theBlip ) == thePlayer then 
            return theBlip 
        end 
    end 
    return false 
end 

Share this post


Link to post

Because you're trying to get element which the player is attached to. To get the elements which are attached to the player, use this function:

getAttachedElements 

But I'd recommend to use tables to keep information which player the blip belongs to. Then you can just take the blip value out of the table and destroy it when the player quits.

In addition, you're needlessly getting the name of the player and then getting the player from name. Lines with getPlayerName and getPlayerFromName could be simplified to an expression:

player = source 

And finally, it's better to make the variables local by using the 'local' keyword. Local variables are faster and only exist in the scope they were created in (such as function). So the code should look something like this:

player_blips = {} --creating a table 
  
function playerJoin() 
    local blip = createBlipAttachedTo(source) 
    player_blips[source] = blip --storing the blip into the table under the player element key 
end 
addEventHandler("onPlayerJoin", root, playerJoin) 
  
function playerQuit() 
    local blip = player_blips[source] --retrieving the blip from the table 
    destroyElement(blip) 
    player_blips[source] = nil --since we don't need the information about the blip in the table anymore, we remove it to free the memory 
end 
addEventHandler("onPlayerQuit", root, playerQuit) 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.