Mirou123 Posted December 8, 2013 Posted December 8, 2013 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
TrapLord Studios™ Posted December 8, 2013 Posted December 8, 2013 DoorPositions = { [1]={ x, y, z }, [2]={ x, y, z }, [3]={ x, y, z }, [4]={ x, y, z }, }
Mirou123 Posted December 11, 2013 Author Posted December 11, 2013 Thanks but how can I loop through them?
TrapLord Studios™ Posted December 11, 2013 Posted December 11, 2013 What do you mean "Loop Through them" ?
DNL291 Posted December 11, 2013 Posted December 11, 2013 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
Mirou123 Posted December 11, 2013 Author Posted December 11, 2013 I want to compare each position in that table with the player's position then outputs the position or anything of the closest door
Castillo Posted December 11, 2013 Posted December 11, 2013 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.
Mirou123 Posted December 12, 2013 Author Posted December 12, 2013 Thanks,But it's not working it doesn't show any errors nor do anything IG
Gallardo9944 Posted December 12, 2013 Posted December 12, 2013 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)
Castillo Posted December 12, 2013 Posted December 12, 2013 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".
Mirou123 Posted December 12, 2013 Author Posted December 12, 2013 Yeah I did.But I think maybe I am doing something wrong when calling the function? this is what I did addCommandHandler("door",IsNearDoor)
Castillo Posted December 12, 2013 Posted December 12, 2013 Can you post or send me via PM the full script?
Mirou123 Posted December 12, 2013 Author Posted December 12, 2013 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
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