KamilPL Posted April 1, 2013 Share Posted April 1, 2013 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
golanu21 Posted April 1, 2013 Share Posted April 1, 2013 you are put the create blip and destroy blip in same event Link to comment
][sojn][ Posted April 1, 2013 Share Posted April 1, 2013 function playerQuit() destroy(blip) end addEventHandler("onPlayerQuit", root, playerQuit) Link to comment
TAPL Posted April 1, 2013 Share Posted April 1, 2013 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
DiSaMe Posted April 1, 2013 Share Posted April 1, 2013 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now