Mathias Lui Posted July 1, 2016 Share Posted July 1, 2016 Hey, I'm trying to script a gate, which moves, when a Player types /gate1. The player has to be in a specific range to a certain point, so that he can't open the gate, whereever he is. The gate spawns, but I can't open it. It doesn't happen anything at all. The script is client side. I tried it with different types of targets (such as localPlayer, getLocalPlayer(), source, root,...). Please help Also I wanted to make a switch, so there's 1 command for open AND closing the gate. I began with a boolean (gate1_open). I didn't know how to start. I'm a very beginner! I'm a bit confused about client and serverside (with source or localPlayer or whatever) gate1 = createObject ( 975, -2695.5, 1274.8000488281 , 56.099998474121, 0, 0, 180) gate1_open = false function gate_func() function openGate1() if (isElementInRange(getLocalPlayer(), -2690.70654, 1268.74756, 55.94558, 2) == true) then moveObject(gate1, 4000, -2695.5, 1274.8000488281, 52.499998474121) if (gate1_open == false) then gate1_open = true else gate1_open = false end else outputChatBox("Stelle Dich in den Wirkungsbereich!", getLocalPlayer(), 255, 0, 0, false) end end end addEventHandler("onResourceStart", getRootElement(), gate_func) addCommandHandler("gate1", openGate1) Link to comment
LabiVila Posted July 1, 2016 Share Posted July 1, 2016 Very first of all, second argument of `outputChatBox` in client-sided shouldn't be a player, just the color codes, and your fifth argument isn't needed since you added no hex color codes into your sentence. Secondly have you declared the 'isElementInRange' function? If that's all your code, it's invalid as well. Third, you don't have to use both 'onResourceStart' and the command handler at the same function literally. If you use command handler only, it would be called when you write the /gate1 even if there's no onResourceStart. If this isn't all your code, try to post it all Link to comment
Mathias Lui Posted July 1, 2016 Author Share Posted July 1, 2016 Hi, unfortuately, this is all my code. I don't know how to declare 'isElementInRange'. How do I do that? I thought it would be done by typing getLocalPlayer(). I guess I fixed the other issues. gate1 = createObject ( 975, -2695.5, 1274.8000488281 , 56.099998474121, 0, 0, 180) gate1_open = false function gate_func() function openGate1() if (isElementInRange(getLocalPlayer(), -2690.70654, 1268.74756, 55.94558, 2) == true) then moveObject(gate1, 4000, -2695.5, 1274.8000488281, 52.499998474121) if (gate1_open == false) then gate1_open = true else gate1_open = false end else outputChatBox("Stelle Dich in den Wirkungsbereich!", 255, 0, 0) end end end addCommandHandler("gate1", openGate1) Link to comment
LabiVila Posted July 1, 2016 Share Posted July 1, 2016 If you want to use isElementInRange then you have to include this somewhere in your code: function isElementInRange(ele, x, y, z, range) if isElement(ele) and type(x) == "number" and type(y) == "number" and type(z) == "number" and type(range) == "number" then return getDistanceBetweenPoints3D(x, y, z, getElementPosition(ele)) <= range -- returns true if it the range of the element to the main point is smaller than (or as big as) the maximum range. end return false end I'll re-edit this code in a bit, gonna edit this post later EDIT: gate1 = createObject (975, -2685.5, 1274.8, 56.1, 0, 0, 180) gate1_open = false --You have to include this function isElementInRange(ele, x, y, z, range) if isElement(ele) and type(x) == "number" and type(y) == "number" and type(z) == "number" and type(range) == "number" then return getDistanceBetweenPoints3D(x, y, z, getElementPosition(ele)) <= range -- returns true if it the range of the element to the main point is smaller than (or as big as) the maximum range. end return false end addCommandHandler ("gate1", function () if (isElementInRange (localPlayer(), -2690.7, 1268.7, 55.95, 2) == true) then --Dunno if this works, I always did the range-check manually moveObject (gate1, 4000, -2695.5, 1274.8, 52.5) if (gate1_open == false) then --?! gate1_open = true--?! else--?! gate1_open = false--?! end else outputChatBox ("Stelle dich in den Wirk...", 255, 0, 0) end end ) the --?! I wrote them because I don't know why you wrote those, if you want to do something later with them then fine, but if this all you want they're useless Link to comment
Mathias Lui Posted July 1, 2016 Author Share Posted July 1, 2016 Thanks, Okay, I don't see any '--?!'. That looks complicated AF. Are there any courses of lua scripting in MTA? I only found some basic courses. Link to comment
LabiVila Posted July 1, 2016 Share Posted July 1, 2016 There is afaik, it's MtaScriptingSchool or soemthing liek that, check this guy's channel, it helped me a lot back then: https://www.youtube.com/channel/UCzxhXI ... yAot6ALS_A Link to comment
Mathias Lui Posted July 2, 2016 Author Share Posted July 2, 2016 I already saw a video of him, but I didn't see the other videos he made. They're helpful indeed. Thanks Link to comment
G-Stefan Posted July 2, 2016 Share Posted July 2, 2016 So if u want to learn to script use: https://wiki.multitheftauto.com/wiki/Main_Page U can learn alot of stuff from wiki and is useful af https://www.lua.org/ Basic lua language(IMPORTANT) After learning lua try this: https://wiki.multitheftauto.com/wiki/Scripting_Introduction Then Try to start with basic scripts(showing a message etc..) After that try to make ur scripts more advanced GUI is very inportant in MTA https://wiki.multitheftauto.com/wiki/Introduction_to_Scripting_the_GUI And wiki functions has alot of examples,look at these too 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