Jump to content

problem with function that removes blip when distance >= 300


Adde

Recommended Posts

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 

Link to comment
  • Moderators

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) 

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

Link to comment

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.

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