Mathias Lui Posted July 2, 2016 Share Posted July 2, 2016 Hi, I want to script a resource which makes a player able to get his distance to another player. I could need help. It's in german, so entfernung = distance Meter = meters I have this so far: function getDistance(target) x, y, z = getElementPosition(client) x1, y1, z1 = getElementPosition(getPlayerFromPartialName(target)) distance = getDistanceBetweenPoints3D( x, y, z, x1, y1, z1) outputChatBox("Entfernung: "..distance.." Meter", client, 0, 255, 0) end addCommandHandler("entfernung target", getDistance) Link to comment
Noki Posted July 2, 2016 Share Posted July 2, 2016 function getDistance(playerSource, _, target) local x, y, z = getElementPosition(playerSource) local x1, y1, z1 = getElementPosition(getPlayerFromPartialName(target)) local distance = getDistanceBetweenPoints3D(x, y, z, x1, y1, z1) outputChatBox("Entfernung: "..distance.." meters", playerSource, 0, 255, 0) end addCommandHandler("entfernung", getDistance) You were definitely on the right track. However, addCommandHandler has two parameters, the player who typed the command and the command name (which we usually ignore). client, the variable which you were using, is used for events that have been triggered from the client-side. It's an easy mistake to make and I used to do it myself. Only after those two parameters do any arguments we type after the command (eg: /command arg1 arg2) get processed. Now you were declaring your variables as global. Which we don't really do as they're not needed outside of that function. Local variables get destroyed after the code block in which they're in ends. In the case of your script, they will get destroyed when the function ends. The actual command handler, we just need the command name. We don't need to specify anything that comes after. We do that in the handler function itself. I also don't think the command system plays well with spaces (as they're what are used to separate arguments). The only thing currently wrong with your code is it will output errors when it can't find a player with that name. A few simple if-statements could solve that. Link to comment
Mathias Lui Posted July 2, 2016 Author Share Posted July 2, 2016 Thanks for your reply, First of all, I'm pretty new to this stuff. I don't know why you have to write them in the parameters brackets. Couldn't I leave it blank? Why or why not? I also don't know, when to use which word for the local player. Could you make a list? (containing what words (eg. source, client, localPlayer, playerSource, etc) you use for which purpose) Also, why did you write '_' in the parameters brackets? And where/how do I specify the arguments? And what if statements do you mean? What should they do? Sorry for that questions. I hope you can answer all/some of these. Link to comment
Noki Posted July 2, 2016 Share Posted July 2, 2016 I don't know why you have to write them in the parameters brackets. Couldn't I leave it blank? Why or why not? Think of it like a machine. You put something into the machine to get something out. What you're doing with these parameters is telling the machine what you're going to be "feeding" it. I also don't know, when to use which word for the local player. Could you make a list? (containing what words (eg. source, client, localPlayer, playerSource, etc) you use for which purpose) They are called predefined variables. You can find all predefined variables here - https://forum.multitheftauto.com/viewtopic.php?f=91&t=39678. I just used 'playerSource' because that's what I usually use for command handlers. You can put anything you like in there. Also, why did you write '_' in the parameters brackets? It's just a variable. You can put anything in there. I have always worked with the '_' (underscore) as a placeholder that I won't use. And where/how do I specify the arguments? In the brackets of a function. function hello(argument1, argument2) And what if statements do you mean? What should they do? They should check if a player was found from that name. local player = getPlayerFromPartialName(target) if (not player) then -- We haven't found a player outputChatBox(playerSource, "We couldn't find a player with that name", 255, 0, 0) return end Sorry for that questions. I hope you can answer all/some of these. No need to be sorry. We've all got to start somewhere. Link to comment
Mathias Lui Posted July 2, 2016 Author Share Posted July 2, 2016 Thanks! You helped me alot! I don't know if it is working. I am currently alone on my server, so I can't test it. But the error message works already! I also get the error message when I try to get the distance to myself Also in-game you can do /ap to set your alpha, is there something like setPlayerVisible or so? So that the script can control the alpha of a player.. Link to comment
Tails Posted July 2, 2016 Share Posted July 2, 2016 You can test it with an element that's not a player like a ped for example. The error was: 'expected element' so all you need to do is add a check: if isElement(target) then Like so: function getDistance(playerSource, _, target) local x, y, z = getElementPosition(playerSource) local target = getPlayerFromPartialName(target) if isElement(target) then local x1, y1, z1 = getElementPosition(target) local distance = getDistanceBetweenPoints3D(x, y, z, x1, y1, z1) outputChatBox("Distance between you and "..getPlayerName(target)..": "..distance.." meters", playerSource, 0, 255, 0) else outputChatBox("We couldn't find a player with that name", playerSource, 255, 0, 0) end end addCommandHandler("entfernung", getDistance) For the player alpha you can do setElementAlpha(playerSource,150) Link to comment
Mathias Lui Posted July 2, 2016 Author Share Posted July 2, 2016 Ah thanks, didn't see that 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