Jump to content

Lua Tutorials


Guest mat000

Recommended Posts

Hello, i can't execute the code of the Tutorial 1 : User selectables warp points

I haven't warnings on the 'start ' command but this don't work :(

I press 'i' but i doesn't show the cursor...

Thanks.

Link to comment
And can you write a few tweaks here and there ?

Thanks !

sure ;)

-- :: MTA Scripting Tutorial I - User selectable warp points 
-- [url=http://www.mtasa.com/tutorial1.html]http://www.mtasa.com/tutorial1.html[/url] 
-- 
-- (C) The MTA Team, 2007 
-- [url=http://www.mtasa.com]http://www.mtasa.com[/url] 
  
function Script_onMapLoad () 
    for index, player in getElementsByType ( "player" ) do              --For each player in the server, 
        bindKey ( player, "i", "down", modeIO )             --Bind the player's "i" key to the function "modeIO" when the key is pressed 
    end 
end 
addEventHandler ( "onMapLoad", getRootElement(), Script_onMapLoad )         --This event tells what happens when the map loads. 
  
function Script_onPlayerJoin () 
    bindKey ( source, "i", "down", modeIO )                 --Bind the player's "i" key to the function "modeIO" when the key is pressed 
end 
addEventHandler ( "onPlayerJoin", getRootElement(), Script_onPlayerJoin )       --This event tells what happens when a player joins. 
  
  
function modeIO ( source, key, keyState )                       --This function toggles the cursor on/off 
    if isCursorShowing ( source ) then                      --If cursor was already showing, 
        showCursor ( source, false )                        --then hide it; 
    else                                        --if it wasn't showing, 
        showCursor ( source, true )                     --then show it. 
    end 
end 
  
function Script_onPlayerClick ( key, keyState, element, x, y, z ) 
    if keyState == "up" then return end                     --Don't do anything if he's releasing the button. 
    if key == "left" then                               --If it's left-click: 
        destroyElement ( getElementData ( source, "teleport" ) )        --Destroy his teleport point, if any 
        local theMarker = createMarker ( x, y, z, "cylinder", 2, 0, 255, 0, 50 )    --Create a cylindric marker 
        setElementData ( theMarker, "type", "teleport" )                --Mark the cylinder as a teleport 
        setElementData ( source, "teleport", theMarker )                --Link the creator to the teleport 
        setElementData ( theMarker, "owner", source )               --Link the teleport to its creator 
  
    elseif key == "right" then                          --If it's right-click 
        destroyElement ( getElementData ( source, "destination" ) )     --Destroy his destination point, if any 
        local theMarker = createMarker ( x, y, z+1, "corona", 1, 0, 255, 255, 50 )  --Create a glowing corona 
        setElementData ( theMarker, "type", "destination" )         --Mark the corona as a destination point 
        setElementData ( source, "destination", theMarker )         --Link the creator to the teleport 
        setElementData ( theMarker, "owner", source )               --Link the teleport to its creator 
  
    elseif key == "middle" then                         --If it's middle-click 
        setElementPosition ( source, x, y, z+1 )                --Teleport the player to where he clicked. 
    end 
end 
addEventHandler ( "onPlayerClick", getRootElement(), Script_onPlayerClick )     --This event tells what happens when a player clicks on the screen with the cursor. 
  
function Script_onMarkerHit ( player ) 
    if getElementData ( source, "type" ) == "teleport" then             --If the marker is a teleport point, 
        local owner = getElementData ( source, "owner" )            --Get the owner linked to the teleport point. 
        local destination = getElementData ( owner, "destination" )     --Get the destination point linked to the owner. 
        if destination then                         --If destination point exists, 
            local x, y, z = getElementPosition ( destination )      --Get the destination point's position. 
            setElementPosition ( player, x, y, z )              --Put the player there. 
        end 
    end 
end 
addEventHandler ( "onMarkerHit", getRootElement(), Script_onMarkerHit )     --This event tells what happens if a player steps inside a marker. 
  
function Script_onPlayerQuit ( reason ) 
    destroyElement ( getElementData ( source, "teleport" ) )            --Destroy his teleport point, if any 
    destroyElement ( getElementData ( source, "destination" ) )         --Destroy his destination point, if any 
end 
addEventHandler ( "onPlayerQuit", getRootElement(), Script_onPlayerQuit )       --This event tells what happens if a player disconnects. 

tell me if it works

Link to comment
Because some of the functions/events/other stuff was changed since the tutorial was made :P

yep, that's right, for example command/event handlers, setTimers, bindKeys have been changed, you don't pass a string (the function name) anymore but a pointer to the function, also the functions need to be written before the event handlers

I noticed another mistake (since MTA now uses a newer version of LUA), you should update your script to make sure it works correctly

function Script_onMapLoad () 
    for index, player in ipairs ( getElementsByType ( "player" ) ) do              --For each player in the server, 
        bindKey ( player, "i", "down", modeIO )             --Bind the player's "i" key to the function "modeIO" when the key is pressed 
    end 
end 
  

Link to comment
I still get errors with this script

WARNING: script.lua: Bad argument @ 'destroyElement'

for all the lines that have destroyElement() on it.

you can ignore that error, or if you want it to disappear use this updated script:

-- :: MTA Scripting Tutorial I - User selectable warp points 
-- [url=http://www.mtasa.com/tutorial1.html]http://www.mtasa.com/tutorial1.html[/url] 
-- 
-- (C) The MTA Team, 2007 
-- [url=http://www.mtasa.com]http://www.mtasa.com[/url] 
  
function Script_onMapLoad () 
    for index, player in ipairs ( getElementsByType ( "player" ) ) do              --For each player in the server, 
        bindKey ( player, "i", "down", modeIO )             --Bind the player's "i" key to the function "modeIO" when the key is pressed 
    end 
end 
addEventHandler ( "onMapLoad", getRootElement(), Script_onMapLoad )         --This event tells what happens when the map loads. 
  
function Script_onPlayerJoin () 
    bindKey ( source, "i", "down", modeIO )                 --Bind the player's "i" key to the function "modeIO" when the key is pressed 
end 
addEventHandler ( "onPlayerJoin", getRootElement(), Script_onPlayerJoin )       --This event tells what happens when a player joins. 
  
  
function modeIO ( source, key, keyState )                       --This function toggles the cursor on/off 
    if isCursorShowing ( source ) then                      --If cursor was already showing, 
        showCursor ( source, false )                        --then hide it; 
    else                                        --if it wasn't showing, 
        showCursor ( source, true )                     --then show it. 
    end 
end 
  
function Script_onPlayerClick ( key, keyState, element, x, y, z ) 
    if keyState == "up" then return end                     --Don't do anything if he's releasing the button. 
    if key == "left" then                             --If it's left-click: 
        local theMarker = getElementData ( source, "teleport" ) 
        if theMarker then 
            destroyElement ( getElementData ( source, "teleport" ) )        --Destroy his teleport point, if any 
        end 
        theMarker = createMarker ( x, y, z, "cylinder", 2, 0, 255, 0, 50 )    --Create a cylindric marker 
        setElementData ( theMarker, "type", "teleport" )                --Mark the cylinder as a teleport 
        setElementData ( source, "teleport", theMarker )                --Link the creator to the teleport 
        setElementData ( theMarker, "owner", source )               --Link the teleport to its creator 
  
    elseif key == "right" then                          --If it's right-click 
        local theMarker = getElementData ( source, "destination" ) 
        if theMarker then 
            destroyElement ( getElementData ( source, "destination" ) )     --Destroy his destination point, if any 
        end 
        theMarker = createMarker ( x, y, z+1, "corona", 1, 0, 255, 255, 50 )  --Create a glowing corona 
        setElementData ( theMarker, "type", "destination" )         --Mark the corona as a destination point 
        setElementData ( source, "destination", theMarker )         --Link the creator to the teleport 
        setElementData ( theMarker, "owner", source )               --Link the teleport to its creator 
  
    elseif key == "middle" then                         --If it's middle-click 
        setElementPosition ( source, x, y, z+1 )                --Teleport the player to where he clicked. 
    end 
end 
addEventHandler ( "onPlayerClick", getRootElement(), Script_onPlayerClick )     --This event tells what happens when a player clicks on the screen with the cursor. 
  
function Script_onMarkerHit ( player ) 
    if getElementData ( source, "type" ) == "teleport" then             --If the marker is a teleport point, 
        local owner = getElementData ( source, "owner" )            --Get the owner linked to the teleport point. 
        local destination = getElementData ( owner, "destination" )     --Get the destination point linked to the owner. 
        if destination then                         --If destination point exists, 
            local x, y, z = getElementPosition ( destination )      --Get the destination point's position. 
            setElementPosition ( player, x, y, z )              --Put the player there. 
        end 
    end 
end 
addEventHandler ( "onMarkerHit", getRootElement(), Script_onMarkerHit )     --This event tells what happens if a player steps inside a marker. 
  
function Script_onPlayerQuit ( reason ) 
    destroyElement ( getElementData ( source, "teleport" ) )            --Destroy his teleport point, if any 
    destroyElement ( getElementData ( source, "destination" ) )         --Destroy his destination point, if any 
end 
addEventHandler ( "onPlayerQuit", getRootElement(), Script_onPlayerQuit )       --This event tells what happens if a player disconnects. 

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...