MarbleXeno Posted February 18, 2020 Share Posted February 18, 2020 (edited) I'm just learning to program in Lua. I'm writing simple scripts. Why doesn't this work? No error appears in the console (same in debugscript). I don't know what's going on, can anyone help me? When the script is enabled, nothing happens if I delete the "function createCar" script, it works normally script.Lua: function createCar() local nFirst = 400 local nSecond = math.random(20, 50) local result = nFirst + nSecond if result > 20 then local x, y, z = getElementPosition(getPlayerFromName("Xeno")) local car = createVehicle(result, x, y, z) outputChatBox("Vehicle "..getVehicleName(car).." has been created") end end meta.xml: <meta> <script src="script.Lua" type="server" /> </meta> Edited February 18, 2020 by MarbleXeno Link to comment
Skadoosh Posted February 18, 2020 Share Posted February 18, 2020 Its because this is a function and to execute the code inside a function , you have to trigger it using a command (addCommandHandler) or an event (addEventHandler). You can fix your code by just adding addCommandHandler('car',root,createCar) Link to comment
MarbleXeno Posted February 18, 2020 Author Share Posted February 18, 2020 Thanks for helping me! 1 Link to comment
Scripting Moderators ds1-e Posted February 18, 2020 Scripting Moderators Share Posted February 18, 2020 (edited) 1 hour ago, Skadoosh said: Its because this is a function and to execute the code inside a function , you have to trigger it using a command (addCommandHandler) or an event (addEventHandler). You can fix your code by just adding addCommandHandler('car',root,createCar) Wrong. bool addCommandHandler ( string commandName, function handlerFunction [, bool restricted = false, bool caseSensitive = true ] ) root shouldn't be there, but anyways you don't need to add command to execute this function. You can simply add createCar() under your function, and it will be processed. Edited February 18, 2020 by majqq Link to comment
Skadoosh Posted February 19, 2020 Share Posted February 19, 2020 13 hours ago, majqq said: Wrong. bool addCommandHandler ( string commandName, function handlerFunction [, bool restricted = false, bool caseSensitive = true ] ) root shouldn't be there, but anyways you don't need to add command to execute this function. You can simply add createCar() under your function, and it will be processed. Oki , i haven't been coding since a month on MTA cuz of my exams and yea we can also execute the function by just calling it. 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