-Note Posted July 21, 2020 Share Posted July 21, 2020 I have this code (client-side) which gets the nearest element but the problem was something I don't want. I mean the element it gets is not the nearest but the second nearest element. Example: Me (localPlayer) Player 1 (v) Player 2 (v) the camera target will be element Player 2 not element player 1 which what I want. function exampleCode() for ik, v in ipairs (getElementsByType("player")) do if v ~= localPlayer then x, y, z = getElementPosition(localPlayer) xv, yv, zv = getElementPosition(v) distance = getDistanceBetweenPoints3D(x, y, z, xv, yv, zv) if distance < 10 then setCameraTarget(xv, yv, zv) end end end end Link to comment
Scripting Moderators ds1-e Posted July 21, 2020 Scripting Moderators Share Posted July 21, 2020 1 hour ago, -Note said: I have this code (client-side) which gets the nearest element but the problem was something I don't want. I mean the element it gets is not the nearest but the second nearest element. Example: Me (localPlayer) Player 1 (v) Player 2 (v) the camera target will be element Player 2 not element player 1 which what I want. function exampleCode() for ik, v in ipairs (getElementsByType("player")) do if v ~= localPlayer then x, y, z = getElementPosition(localPlayer) xv, yv, zv = getElementPosition(v) distance = getDistanceBetweenPoints3D(x, y, z, xv, yv, zv) if distance < 10 then setCameraTarget(xv, yv, zv) end end end end Faster, and working way. function getNearestPlayer() local pX, pY, pZ = getElementPosition(localPlayer) local playersTable = getElementsWithinRange(pX, pY, pZ, 10, "player") local nearestPlayer = false local currentPlayer = false for i = 1, #playersTable do currentPlayer = playersTable[i] if currentPlayer ~= localPlayer then nearestPlayer = currentPlayer break end end return nearestPlayer end Link to comment
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