Jump to content

Help with function


Mirou123

Recommended Posts

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 :)

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

Link to comment

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) 

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