Jump to content

Blip not getting removed on quit.


mateplays

Recommended Posts

Hi guys. I have some trouble.

I want to get rid of any blips attached to a player, here is the code:

addEventHandler("onPlayerQuit",getRootElement(),
    function()
        local blip = getBlipAttachedTo(source) 
        if blip then 
            destroyElement(blip) 
        end 
    end
)


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 

What causes the problem? Please help me.

Link to comment

Your getBlipAttachedTo function only returns the first blip that it finds attached to the player.  (Also I wouldn't loop through every blip in the server as this can have an impact on performance depending on how many blips there are)

What I would advise is looping through the attached elements of the player , adding all the blips to a table then returning the table(instead of only returning the first found attached blip).  Then in your onPlayerQuit event, you loop through that table destroying everything. Pretty much the code I have below, hope this helps you!


 

addEventHandler("onPlayerQuit",getRootElement(),
    function()
        local blips = getBlipsAttachedTo(source) 
        if blips then 
            for i,theBlip in ipairs(blips) do
                destroyElement(theBlip) 
            end
        end 
    end
)

function getBlipsAttachedTo( thePlayer ) 
    local attached = {}
    local blips = getAttachedElements(thePlayer)
    for k, theBlip in ipairs( blips ) do 
        if isElement(theBlip) and getElementType(theBlip) == 'blip' then 
            table.insert(attached,theBlip)
        end 
    end 
    return attached 
end 


 

Link to comment
9 minutes ago, mateplays said:

Thanks for the code, but... somehow it still does not work... i tried putting this into a different resource but it still does not get rid of any blips, and it does not output any errors to debugscript

Just tested it, works perfectly for me when I attached a blip to myself via command.
Could you show your current code ?

Link to comment

So... Here is the code. I call this function once a new player joins into the party. This is client side:

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 

function belepveAGecibe()
    for k,v in pairs(getElementsByType("player")) do
        if (getElementData(v, "inPartyOf") == getElementData(localPlayer, "inPartyOf")) then
            if v ~= localPlayer then
                if not getBlipAttachedTo(v) then
                    x, y, z = getElementPosition(v)
                    blipToPartyMembers = createBlipAttachedTo(v, 35)
                    setElementData(blipToPartyMembers, 'tooltipText', getPlayerName(v))
                end
            end
        end
    end
end
addEvent("belepveNagyon",true)
addEventHandler("belepveNagyon",getRootElement(), belepveAGecibe)

And i use this on server side:

addEventHandler("onPlayerQuit",getRootElement(),
    function()
        local blips = getBlipsAttachedTo(source) 
        if blips then 
            for i,theBlip in ipairs(blips) do
                destroyElement(theBlip) 
            end
        end 
    end
)

function getBlipsAttachedTo( thePlayer ) 
    local attached = {}
    local blips = getAttachedElements(thePlayer)
    for k, theBlip in ipairs( blips ) do 
        if isElement(theBlip) and getElementType(theBlip) == 'blip' then 
            table.insert(attached,theBlip)
        end 
    end 
    return attached 
end 

Edit: Got it to work using this solution: 

addEventHandler("onClientPlayerQuit",getRootElement(),
    function()
        if getBlipAttachedTo(source) then 
            destroyElement(blipToPartyMembers)
        end 
    end
)

 

Edited by mateplays
-
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...