ManeXi Posted June 3, 2016 Share Posted June 3, 2016 (edited) I want to get distance between two players but the code I made it doesn't work very well... My objective is making a code where I write "/getdist (Player A name) (Player B name)" and getting distance between player A and B in the chatbox function distCmd( cmd, pla1x, pla1y, pla1z, pla2x, pla2y, pla2z) if cmd == "getdist" then player1 = tostring(getPlayerFromName("")) player2 = tostring(getPlayerFromName("")) pla3 = getPlayerFromName(player1) pla4 = getPlayerFromName(player2) pla1x, pla1y, pla1z = getElementPosition ( pla3 ) pla2x, pla2y, pla2z = getElementPosition ( pla4 ) if pla1x then outputChatBox ( "The distance between pla1 and pla2 is "..tostring(getDistanceBetweenPoints3D ( pla1x, pla1y, pla1z, pla2x, pla2y, pla2z )) ) else outputChatBox("Another Error", player) end else outputChatBox("Error", player) end end addCommandHandler("getdist", distCmd) Edited June 5, 2016 by Guest Link to comment
Noki Posted June 3, 2016 Share Posted June 3, 2016 You can use getPlayerFromPartialName. Link to comment
ManeXi Posted June 3, 2016 Author Share Posted June 3, 2016 This is the code now: function getPlayerFromPartialName(name) local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil if name then for _, player in ipairs(getElementsByType("player")) do local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower() if name_:find(name, 1, true) then return player end end end end function distCmd( cmd, pla1x, pla1y, pla1z, pla2x, pla2y, pla2z) if cmd == "getdist" then player1 = tostring(getPlayerFromName("")) player2 = tostring(getPlayerFromName("")) pla3 = getPlayerFromPartialName(player1) pla4 = getPlayerFromPartialName(player2) pla1x, pla1y, pla1z = getElementPosition ( pla3 ) pla2x, pla2y, pla2z = getElementPosition ( pla4 ) if pla1x then outputChatBox ( "The distance between pla1 and pla2 is "..tostring(getDistanceBetweenPoints3D ( pla1x, pla1y, pla1z, pla2x, pla2y, pla2z )) ) else outputChatBox("Another Error", player) end else outputChatBox("Error", player) end end addCommandHandler("getdist", distCmd) Idk, if I did it rightly or not but the code is not working Link to comment
Noki Posted June 3, 2016 Share Posted June 3, 2016 The problem lies in the parameters you have for the distCmd function. You have all the arguments for coordinates specified, but not the ones for the player names. You can also remove the player1 and player2 lines as they serve no purpose. You also don't have 'player' defined in your code. If we are talking about the player who typed the command, we need to specify it in the function's arguments. function distCmd(player, cmd, pla3, pla4) You can also remove the if statement that checks if cmd is "getdist". You're using a command handler, so the function will only be called if you type '/getdist'. However, if you were using the onPlayerCommand event, you would need that if statement. Link to comment
ManeXi Posted June 5, 2016 Author Share Posted June 5, 2016 Now it's working but I don't understand very well how the command "getdist" detects the next 2 players. Link to comment
Noki Posted June 6, 2016 Share Posted June 6, 2016 You give the partial names of the two players when you type the command. So you could write "/getdist noki overmind" and getPlayerFromPartialName would try and resolve these partial names into player elements. The function works by checking all players and seeing if the string given (the partial name you entered in /getdist) is a close match to the name of an online player. If there is a match, it will return a player element. If my name in-game was "[MTA]Noki", it would still resolve "noki" into my player element. The same would go for you. "overmind" would get resolved to your player element, even if that wasn't your exact name. 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