Jump to content

[Help] Script doesn't work


MarbleXeno

Recommended Posts

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 by MarbleXeno
Link to comment
  • Scripting Moderators
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 by majqq
Link to comment
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...