Jump to content

destroy blip


KamilPL

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) 
  

Link to comment
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 

Link to comment

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) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...