Search the Community
Showing results for tags 'variable'.
-
Lua Language Server - Definition files The Lua language server is a powerful tool that enhances the development experience for Lua programming. It provides a comprehensive set of code editing features, including suggestions, auto-completion, and error checking. With the Lua language server, developers can effortlessly navigate through their resource files, access documentation easily, and ensure code correctness by giving warnings. Why should you care? The language server will inform you about all sorts of problems: type mismatches, missing function arguments, missing variables, etc. You have access to a lot of MTA syntax/autocomplete out of the box. The syntax information will remain while writing. You do not have to restart your resource so often in order to validate if everything is working. Type validation Having value type validation in your code editor is one of the main key features of the Lua Language Server. When working with variables, parameters, and arguments in Lua, you are not restricted to specific value types. This flexibility can make mistakes more likely to happen. However, being able to validate those mistakes instantly saves you a lot of time and frustration. Type annotations for your own functions Adding type annotations to your own functions can help improve validation and catch logic mistakes. It is particularly useful when calling functions from different parts of your code, as the annotations provide clarity on the expected input (arguments) and output (return values). Additionally, comments that are placed above or adjacent to a variable or function are visible when hovering over them in another file or line. This can provide helpful information and context when working with the code. How that looks like: How can I quickly add annotations in less than a second? Open the spoiler: AddEventHandler auto-complete Most MTA addEventHandler functions have full eventName autocompletion. And the attached anonymous function is fully autocompleted and typed as well. Navigation features of Lua Language Server It can be time consuming to find out where a (global) function or variable is located. Being able to jump right to it, saves you a lot of time. Other information which you can find in the readme Installation for the Lua Language Server How to use the definition files? Known issues Make sure to always have an empty new line at the end of your files, as recommended in this issue. Currently, the Lua server language definition files do not have a clear separation between serverside functions/events and clientside functions/events. However, it is possible to enforce this separation for specific functions if needed. outputChatBox--[[@as outputChatBox_server]]("Serverside", player) In certain cases, certain functions in the Lua server language definition files may return multiple types, even if you have selected a different syntax. To handle this situation, you can use the `cast` or `as` notation to explicitly specify the desired type or adjust the returned type. See `Casting and as` syntax below. Casting and as In certain situations, you may have a strong understanding of the type(s) that a variable or expression will have. This is where the keywords "cast" and "as" come into play. These keywords enable you to explicitly specify the intended type, ensuring proper type handling. local varName = exampleFunc() ---@cast varName string local varName = exampleFunc() ---@cast varName string | number local varName = exampleFunc() --[[@as string]] local varName = exampleFunc() --[[@as string | number]] Download The definition files can be downloaded here.
- 15 replies
-
- 8
-
-
-
- lua
- lua language server
- (and 13 more)
-
I am making a script to use flares on aircraft, the script works fine until two players use a flare at the same time, the declared variables change and one of the flares is not destroyed. Here is a video of my script: I was checking the creator of the flare with an outpuchatbox at the end of the script, and I saw that the creator changed, which means that the variable was declared again. Flares = {} Chaffs = {} function FlarePhys(x, y, z, distance, gz) player = client index = #Flares + 1 Flares[index] = { ["Vehicles"] = { getPedOccupiedVehicle(player) }, ["Lights"] = { createMarker(x,y,z,"corona", 1, 255,0,0) }, ["Flares"] = { createObject(2060, x,y,z) } } setElementData(Flares[index]["Vehicles"][1], "Dismissile", true) setElementCollisionsEnabled(Flares[index]["Flares"][1], false) attachElements(Flares[index]["Lights"][1], Flares[index]["Flares"][1]) moveObject(Flares[index]["Flares"][1], distance*100, x, y, gz +1.5) setTimer ( function() if isElement(Flares[index]["Vehicles"][1]) then removeElementData(Flares[index]["Vehicles"][1], "Dismissile") else destroyElement(Flares[index]["Flares"][1]) destroyElement(Flares[index]["Lights"][1]) end end, 1000, 1 ) setTimer ( function() outputChatBox(getPlayerName(player)) destroyElement(Flares[index]["Flares"][1]) destroyElement(Flares[index]["Lights"][1]) end, 8000, 1 ) end addEvent("UseFlares", true) addEventHandler("UseFlares", getRootElement(), FlarePhys) I need help because i don't know how to make the function work for every player and not for the player that uses the Flare i will appreciate the help, I think i can do this script clientside triggering it for all players but if i do this serverside i will be happy haha.
- 3 replies
-
- serverside
- script
-
(and 2 more)
Tagged with:
-
-- Ez a server oldali script local rigPrice = 50000; function buyMiningRig(player, amount) local money = getPlayerMoney(localPlayer); if (money >= rigPrice) then money - rigPrice outputChatBox("#ffffff[Bitcoin] Sikeresen vettél egy bányászgépet!", 0, 0, 0, true); end end addCommandHandler("buyminingrig", root, buyMiningRig); -- Ez pedig a kliens oldali local maxMiningRigs = 50; local btcPrice = 1500000; local minerToggle = false; local playerBtc = 0; local miningRigs = 0; function mineBtc(howMuch) playerBtc + howMuch; if (minerToggle) then setTimer(mineBtc, 1000, 1, 0.00001); end end function displayBtc() outputChatBox("[Bitcoin] A jelenlegi bitcoinjaid száma:"..playerBtc.. ", és értéke :" ..btcprice/playerBtc); end addCommandHandler("btcamount", root, displayBtc) function toggleMinerOn() if (minerToggle == false) then minerToggle = true; outputChatBox("[Bitcoin] A bányászgép bekapcsolva!"); mineBtc(); elseif (minerToggle == true) then minerToggle = false; outputChatBox("[Bitcoin] A bányászgép kikapcsolva!"); end end addCommandHandler("togglebtc", root, toggleMiner); (A két script ugyan abba a meta-ba van beleírva!) A lényeg annyi lenne, hogy a szerver oldali scriptből hogy tudnék hozzáadni a "miningRigs" változóhoz egy adott mennyiséget. Ha valaki tudna ebbe segíteni, azt megköszönném, legyen bármilyen hosszú a script csak jó legyen.
-
Hello. Is there any difference between using the entity's name instead of a custom name in a variable? local vehicle = createVehicle (...) or local theVehicle = createVehicle (...) Why does the Wiki always avoid using the entity's name as variable? If I use the entity's name instead of a custom name, is this a bad programming method? PS: I am not using OOP.
-
Hello everyone Good friends I have a little doubt I do scripts for mta 3 years ago is from the beginning I wonder one thing about the server side. let's start, we all know that variables used on the server side have their value shared with the whole server. so I can dribble this problem I always create a table for each player so that I can set a variable with a separate value for each player plus the times I feel I do not need to do this anymore I'm not sure, keep reading. I know that Moon performs functions in an ordered way eg if two players call an execution of the same function according to my knowledge on the moon it should run the function twice, once for each right player? and here comes my doubt if I need to make a local variable that loses its value at the end of the function I can simply define a variable or I have to define it inside a table for each player. because although he gave to know how it works, I'm not sure if this gives me my doubts. the function is actually executed 1 time and as soon as it finishes it is executed again for the next player or it is performed doubly at the same time . I know this seems like an unnecessary question more is how I said my fear is by the value of the variables. Thanks Friends excuse me for English evil
-
Dear users, this is the file a.lua: lanciarazzi = { } basirazzi = { } sbarra = 0 function crealanciatori() sbarra = createObject(968, -1526.4400439453, 481.39999389648, 6.905, 0, 270, 0) sbarra.breakable = false lanciarazzi = { createObject(3884, -1496.8, 590.4, 42.05, 0, 0, 225), -- ponte ferroviario nuovo createObject(3884, -1225, 456, 7.05, 0, 0, 90), -- est nuovo } basirazzi = { createObject(3885, -1496.8, 590.4, 42, 0, 0, 0), -- ponte ferroviario nuova createObject(3885, -1225, 456, 7, 0, 0, 0), -- est nuova } end function nonrompere() for ch, v in ipairs(lanciarazzi) do setObjectBreakable(v, false) end for ch, v in ipairs(basirazzi) do setObjectBreakable(v, false) end end This is b.lua: function proteggi(nome, param) nonrompere() addEventHandler("onClientColShapeHit", basesf, inizia) sbarra.move(4000, -1526.4400439453, 481.39999389648, 6.905, 0, -90, 0, 'InOutQuad') outputChatBox("Attenzione: protezione sulla base di San Fierro abilitata.") end function nonproteggere(nome, param) removeEventHandler("onClientColShapeHit", basesf, inizia) sbarra.move(4000, -1526.4400439453, 481.39999389648, 6.905, 0, 90, 0, 'InOutQuad') outputChatBox("Attenzione: protezione sulla base di San Fierro disabilitata.") end addEventHandler("onClientResourceStart", resourceRoot, proteggi) addEventHandler("onClientResourceStop", resourceRoot, nonproteggere) addCommandHandler("sibasesf", proteggi) addCommandHandler("nobasesf", nonproteggere) And this is meta.xml: <meta> <oop>true</oop> <script src="a.lua" type="shared" /> <script src="b.lua" type="client" /> </meta> In function proteggi() and nonproteggere() I can't read the variable sbarra, furthermore the function nonrompere() doesn't work. Where did I go wrong? Thanks
-
My code at the end contains button variables but it defines in 2 cases: 1 - as button1 variable 2 - creates button button1 = guiCreateButton(747, 286, 102, 40, "", false) button2 = guiCreateButton(498, 286, 102, 40, "", false) how can i replace it or prevent creation? i just want make button open and close on pressed key. 1 button changes player skin/2 gives player a car