Jump to content

problem with function that removes blip when distance >= 300


Adde

Recommended Posts

Posted

Hello, I am trying to create a function that removes the blip for the local player when the distance between the players is more than 300. I can´t find out what i am doing wrong :/ help?

client

function distance300thendestroy() 
p1x, p1y, p1z = getElementPosition ( source ) 
p2x, p2y, p2z = getElementPosition ( localPlayer ) 
local dist = getDistanceBetweenPoints2D ( p1x, p1y, p2x, p2y ) 
if ( dist >= 300 ) then 
    local attached = getAttachedElements ( source ) 
    if ( attached ) then 
        for k,element in ipairs(attached) do 
            if getElementType ( element ) == "blip" then 
setElementVisibleTo(element, localPlayer, false) 
end 
end 
end 
end 
end 

  • MTA Team
Posted

You said something is wrong, but you haven't told us what is wrong. What is the issue? Is that your entire code?

Posted

Manawydan: but then all blips Will be removed when anyone are more than 300away from someone?

Oh forgot that part. It doesn't work at all, it doesn't remove the blip. Nothing in debug.

  • Moderators
Posted

Try this:

function blipDistanceChecker() 
    local px, py, _ = getElementPosition ( localPlayer )  
    for k, blip in ipairs ( getElementsByType( "blip" ) ) 
        local bx, by, _ = getElementPosition( blip ) 
        local dist = getDistanceBetweenPoints2D ( px, py, bx, by ) 
        if dist >= 300 then 
            setElementVisibleTo(blip, localPlayer, false) 
        else 
            setElementVisibleTo(blip, localPlayer, true) 
        end 
    end 
end 
setTimer(blipDistanceChecker, 1000, 0) 

Posted
Try this:
function blipDistanceChecker() 
    local px, py, _ = getElementPosition ( localPlayer )  
    for k, blip in ipairs ( getElementsByType( "blip" ) ) 
        local bx, by, _ = getElementPosition( blip ) 
        local dist = getDistanceBetweenPoints2D ( px, py, bx, by ) 
        if dist >= 300 then 
            setElementVisibleTo(blip, localPlayer, false) 
        else 
            setElementVisibleTo(blip, localPlayer, true) 
        end 
    end 
end 
setTimer(blipDistanceChecker, 1000, 0) 

"setElementVisibleTo" is a server function and localPlayer is a client variable.

Here is a code that I have (client-side):

function fixBlipRanges ( ) 
    for index, blip in ipairs ( getElementsByType ( 'blip' ) ) do 
        local id = getBlipIcon ( blip ) 
        if ( id > 1 and id ~= 41 ) then 
            setBlipVisibleDistance ( blip, 300 ) 
        end 
    end 
end 

Posted

If you necessarily want it, use this.

local createBlip_ = createBlip 
function createBlip(x, y, z, icon, size, r, g, b, a, ordering, visibleDistance, visibleTo) 
     return createBlip_(x, y, z, icon, size, r, g, b, a, ordering, (tonumber(visibleDistance) and tonumber(visibleDistance) or 300), visibleTo) 
end 

That's a wrapper function.

Another way is to use https://wiki.multitheftauto.com/wiki/Se ... leDistance.

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...