Jump to content

Help with function


Mirou123

Recommended Posts

Posted

Hi.What I want to do is a bit more complicated but to show you what I need help with I will use a simple example.

Iwant to create a few objects around the town for example doors.I want a command that will loops through all the doors and only the doors and set an ID for each one of them.then tell me if I am close to any of them and the ID of that door.(I am asking for an ID because I want to setCameraMatrix(doorx,doory,doorz,playerx,playery,playerz,) if you know an easier way tell me please.

function IsNearDoor(player) 
local x,y,z = getElementPosition(getLocalPlayer ( )) 
local doorx,doory,doorz = "Idk what to do here" 
local distance = getDistanceBetweenPoints3D(x,y,z,doorx,doory,doorz) 
    if distance < 5 then 
    outputChatBox ("There is a door near you!") 
    setCameraMatrix("The coords of that door",x,y,z) 
    end 
end 

Thank you in advance :)

Posted
for i=1, #DoorPositions do 
    outputChatBox(DoorPositions[i][1], DoorPositions[i][2], DoorPositions[i][3]) 
end 
-- OR 
for _,v in ipairs(DoorPositions) do outputChatBox(v[1], v[2], v[3]) end 

Please do not PM me with scripting related question nor support, use the forums instead.

Posted
function IsNearDoor ( player ) 
    local x, y, z = getElementPosition ( localPlayer ) 
    local nearestDoor 
    local distance 
    for index = 1, #doorPositions do 
        local door = doorPositions [ index ] 
        local doorX, doorY, doorZ = unpack ( door ) 
        if ( distance ) and ( getDistanceBetweenPoints2D ( x, y, z, doorX, doorY, doorZ ) < distance ) then 
            distance = getDistanceBetweenPoints2D ( x, y, z, doorX, doorY, doorZ ) 
            nearestDoor = index 
        elseif ( getDistanceBetweenPoints2D ( x, y, z, doorX, doorY, doorZ ) < 5 ) then 
            distance = getDistanceBetweenPoints2D ( x, y, z, doorX, doorY, doorZ ) 
            nearestDoor = index 
        end 
    end 
  
    if ( nearestDoor ) then 
        outputChatBox ( "There is a door near you!" ) 
        setCameraMatrix ( "The coords of that door", unpack ( doorPositions [ nearestDoor ] ) ) 
    end 
end 

Try it.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

Untested, but must be working though.

DoorPositions = { 
[1]={ 10,20,30 }, 
[2]={ 10,30,20 }, 
[3]={ 100,500,250}, 
[4]={ 0,10,500}, 
} 
  
function checkPosition(player) 
   local px,py,pz = getElementPosition(player) 
   if not px or not py or not pz then 
      outputChatBox("You're nowhere, lol",player) 
      return false 
   end 
   local doorTableNew = DoorPositions -- Create a new variable with the same contains so we don't touch the original table 
   table.sort(doorTableNew,function(one,two) 
     local d1 = getDistanceBetweenPoints3D(px,py,pz,one[1],one[2],one[3]) 
     local d2 = getDistanceBetweenPoints3D(px,py,pz,two[1],two[2],two[3]) 
     return d1 < d2 -- Sort by the shortest distance 
     end) 
   local val = doorTableNew[1] -- Use doorTableNew[#doorTableNew] to get the most distant one if you ever need it 
   outputChatBox("The closest door to you is at : "..val[1]..","..val[2]..","..val[3],player) 
end 
addCommandHandler("check",checkPosition) 

Posted
Thanks,But it's not working :/ it doesn't show any errors nor do anything IG

Make sure your table was called "doorPositions" and not "DoorPositions".

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

Can you post or send me via PM the full script?

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

I will try Gallardo's script first.I hope I won't need to PM and bother you.Thank you everyone

edit:

After a few changes I got the script to work.Thank you so much

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