Jump to content

need a simple gate script asap.


SEV3NS

Recommended Posts

  • MTA Team

Hey,

Welcome to Multi Theft Auto, I hope you enjoy your stay here.

In order to learn to script in Lua scripting language, I suggest you take some time to find the following topic useful for yourself. It contains links to tutorials and information regarding the use of the language in practice. This is essential in order to learn the basics of Lua and then work on your Multi Theft Auto related scripts.

» Lua tutorials, manuals

After you have familiarized yourself with Lua and know how to deploy basic code with it, you may move on to Multi Theft Auto specific scripting.

Up-to-date Multi Theft Auto scripting information, details and help can be found on the Multi Theft Auto Wiki site. Get yourself familiarized with the scripting features Multi Theft Auto offers you by reading articles of which links you can find on the right side of the Wiki front page under the Scripting heading:

You might also find the Tutorials board useful. These are tutorials, manuals and help topics written by community members. If Wiki seems a little bit too hardcore for you at the moment, checking this board should get you started easily.

Feel free to also use our available chatting channels to ask questions and get help in general. These can be found at the IRC Channel Wiki article. Make sure to search for an answer on your preferred search engine first, and through the forum search feature. This way you can find solutions faster and will keep our channels clean of already answered questions!

If you have code which you can post on your topic, that would make it easier for helpers to get a solution for you faster. Keep in mind that we are a public forum and we can only fix your code if you are willing to post it on your topic. This way we give you help and eventually a solution, and if someone else is facing the same issue later they can find a solution to the problem from your topic.

I hope this got you started up!

Link to comment
post your code

heres the code i got so far. it doesnt seem to work though.

i just want the door to simply drop down then rise back up.

if(strcmp(cmdtext,"/opengate", true) == 0) 
  { 
   if (gTeam[sEV3NRS] == player)                     
   { 
         if(PlayerToPoint(15.0, SEV3NRS, 2361, -1272, 24.60000038147)) 
         { 
       MoveObject(object gate1, 2361, -1272, 21, 1.500000); 
     return 1;  
     }     
  }                                                                             
}        
                                                                                 
if(strcmp(cmdtext,"/closegate", true) == 0) 
  {                                                                               
   if (gTeam[sEV3NRS] == player)         
   {                                                                                
         if(PlayerToPoint(15.0, SEV3NRS,2361, -1272, 24.60000038147)) 
         { 
       MoveObject(object gate1, 2361, -1272, 21, 1.500000); 
     return 1; 
     } 
  } 
} 
  gate1 = CreateObject(3084, 2361, -1272, 24.60000038147, 0.0000, 0.0000, 0.0000);//closed 

Link to comment

You're using stuff from other languages here such as "//" that's "--" in LUA,

There's no need for Semi-colomns ";" and is this your full code? because you're missing the function start and end's also the curly brackets aren't in LUA "{" "}",

Example of a code C# compaired with LUA.

// C# Example 
public class Hello1 
{ 
   public static void Main() 
   { 
      System.Console.WriteLine("Hello, World!"); 
   } 
} 
  

-- LUA Example 
  
function Hello1 () 
outputChatBox("Hello, World!") 
end 
  
addEventHandler("onPlayerLogin", getRootElement(), Hello1) 

+ ALSO NOTE:

MoveObject

PlayerToPoint

Stuff like this always start with a lowercase. moveObject, etc.

Take a look on the wiki for all the functions and events.

Server Functions: https://wiki.multitheftauto.com/wiki/Server_Scripting_Functions

Server Events: https://wiki.multitheftauto.com/wiki/Server_Scripting_Events

Client Functions: https://wiki.multitheftauto.com/wiki/Client_Scripting_Functions

Client Events: https://wiki.multitheftauto.com/wiki/Client_Scripting_Events

BTW learn yourself don't use codes you don't understand and look if it's the right game, SAMP and MTA are different.

http://forum.sa-mp.com/showthread.php?t=43930

I hope this helped,

Regards Viruz

Link to comment

That's MTA's API, not really Lua per se. So, really, Hello World would simply look like this;

function helloWorld() 
    print("Hello World"); 
end 

The semi-colon aren't necessarily needed, no. But it's still good practice to use it since they indicate the end of a statement. They're still very useful should you need to execute several statements on a single line, or simply for more effiecent code(though, probably won't have a noticeable impact). It's especially good if you plan to code in more than one language :)

@SEV3NS:

If you search you should find a lot of threads about how to make a script for moving gates. I'm pretty sure there's quite a few resources up on the community as well.

Link to comment

thanks for the help guys. so far im still working on the script. but i also have another question. is there a particular file location i should be placing these files at? i just have it in the same file folder as the map right now. and added the meta for it in the maps meta. also that is the full code. how would i go about typing in the function?

function open gate( )

print (sript?)

end?

Link to comment
function opengate() 
  
if(strcmp(cmdtext,"/opengate", true) == 0) 
  { 
   if (gTeam[sEV3NRS] == player)                     
   { 
         if(playerToPoint(15.0, SEV3NRS, 2361, -1272, 24.60000038147)) 
         { 
       moveObject(object gate1, 2361, -1272, 21, 1.500000); 
     return 1;  
     }     
  }                                                                             
}        
                                                                                 
if(strcmp(cmdtext,"/closegate", true) == 0) 
  {                                                                               
   if (gTeam[sEV3NRS] == player)         
   {                                                                                
         if(playerToPoint(15.0, SEV3NRS,2361, -1272, 24.60000038147)) 
         { 
       moveObject(object gate1, 2361, -1272, 21, 1.500000); 
     return 1; 
     } 
  } 
} 
  gate1 = createObject(3084, 2361, -1272, 24.60000038147, 0.0000, 0.0000, 0.0000); 
  end 

Link to comment

I made this awhile ago just as a template for people to learn and play around with;

local objectID = 980 --Place the object ID here 
local originalX,originalY,originalZ = 282.39999389648,-1319.9000244141,55.639999389648 --Place the coords of the gate here 
local rotationX,rotationY,rotationZ = 0,358.70007324219,34.5 --Place the rotations of the gate here 
  
local moveX,moveY,moveZ = 282.39999389648,-1319.9000244141,50 --Place the coords to move the gate to here 
local moveRotX,moveRotY,moveRotZ = 0,0,0 --Place the rotations for the gate here (if you do not want it to rotate, put: 0,0,0) 
  
local timeToMoveGate = 2000 --Put the time it takes to open/close the gate here (in milliseconds) 
local timeToStayOpen = 4000 --Put the time that the gate stays open until it closes (in milliseconds) 
  
local command = "gate" --Put the command name you want to use here 
local distanceFromGate = 25 --Put the minimal distance from the gate to open it here 
  
--####################################################### 
--####   DO NOT EDIT ANYTHING BELOW HERE             #### 
--####################################################### 
local gateState = false --Leave this at false, you don't need to edit this 
  
thisGate = createObject(objectID,originalX,originalY,originalZ,rotationX,rotationY,rotationZ) --Creates the object and names it 'thisGate' 
  
exports.pool:allocateElement(thisGate) --Let's just leave this here, okay? 
  
local function resetGateState() --Function to reset the state of the gate 
    gateState = false --Sets the gate state to closed 
end 
  
local function closeThisGate() --Function to close the gate 
    moveObject(thisGate,timeToMoveGate,originalX,originalY,originalZ,0 - moveRotX,0 - moveRotY,0 - moveRotZ) --Moves the gate back to the original position 
    setTimer(resetGateState,timeToStayOpen,timeToMoveGate) --Resets the state of the gate after the gate has moved back to it's original position 
end 
  
local function useGate(thePlayer, commandName) --Function that triggers the gate 
    local posX,posY,posZ = getElementPosition(thePlayer) --Gets the position of the gate 
    local distance = getDistanceBetweenPoints3D(originalX,originalY,originalZ,posX,posY,posZ) --Gets the distance between the player and the original position of the gate 
     
    if(distance <= distanceFromGate and gateState == false then --Checks if the player is near enough and if the gate is not open/opening/closing 
        gateState = false --Sets the gate to open/opening/closing 
        moveObject(thisGate,timeToMoveGate,moveX,moveY,moveZ,moveRotX,moveRotY,moveRotZ) --Moves the object to the location that is wanted 
        setTimer(closeThisGate,timeToMoveGate + timeToStayOpen,1) --Sets the times to close the gate again 
    end 
end 
addCommandHandler(command, useGate) 

Hope it helps you learn a bit.

Link to comment
  • MTA Team
thanks for the help guys. so far im still working on the script. but i also have another question. is there a particular file location i should be placing these files at? i just have it in the same file folder as the map right now. and added the meta for it in the maps meta. also that is the full code. how would i go about typing in the function?

function open gate( )

print (sript?)

end?

Please read my first reply to this topic. That will get you started.

Link to comment

ok, so i did some digging and i managed to come up with this script. i tried the scripts above and the didnt seem to work.

only one problem with this scrip, the gate doesnt want to go back up.

function createTheGate () 
  
         myGate1 = createObject ( 3084,2361,-1272,24.60000038147, 0, 0, 90 ) 
  
  
      end 
  
      addEventHandler ( "onResourceStart", getResourceRootElement ( getThisResource () ), createTheGate ) 
  
  
  
  
  
 function openMyGate ( ) 
 moveObject ( myGate1, 2361, -1272, 21,6.0999999046326) 
 end 
 addCommandHandler("gd1",openMyGate) 
  
  
 function movingMyGateBack () 
 moveObject ( myGate1, 2361,-1272,24.60000038147,12.60000038147) 
 end 
 addCommandHandler("gu1",movingMyGateBack) 

Link to comment

I believe you misunderstood the arguments of moveObject, as where you should have entered the time it would take the object to move from Point A to Point B - you had entered your X co-ordinate instead. Do you need something like this?

local isGate1Opened = false 
  
function createGates_Handler() 
    myGate1 = createObject(3084, 2361, -1272, 24.60000038147, 0, 0, 90) 
end 
addEventHandler("onResourceStart", resourceRoot, createGates_Handler) 
  
function openGates_Handler() 
    if(isGate1Opened == false) then 
        moveObject(myGate1, 3000, 2361, -1272, 21) 
        isGate1Opened = true 
    else 
        moveObject(myGate1, 3000, 2361, -1272, 24.60000038147) 
        isGate1Opened = false 
    end 
end 
addCommandHandler("gate1", openGates_Handler) 

If you're going to add a lot of gates, you'll want to use tables. :)

Link to comment

You could do something like this with tables, I just wrote this a bit fast so you'll probably have to adjust it a bit to fit your needs. Of course it would be easier to create a new function and command for every gate, and allow for more precise control of each gate. This is but an example of how you could do it using tables.

--{"Command", ModelID, Closed PosX, Closed PosY, Closed PosZ, Closed RotX, Closed RotY, Closed RotZ, Open PosX, Open PosY, Open PosZ, Open RotX, Open RotY, Open RotZ, MoveTime, theState} 
local gateTable =  
{ 
-- Just add your gates here using the syntax above. Put a , after each array except for the last one. 
{"Gate1", 3084, 2361, -1272, 24.6, 0, 0, 90, 2361, -1272, 21, 0, 0, 90, 3000, false} 
}; 
  
function createGates_Handler() 
    for k, v in ipairs(gateTable) do 
        local a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16 = unpack(v); 
        local theGate = createObject(a2, a3, a4, a5, a6, a7, a8); 
        addCommandHandler(a1, function() 
            local a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16 = unpack(v); 
            if(a16 == false) then 
                moveObject(theGate, a15, a9, a10, a11, a12, a13, 0) 
                table.remove(v, 16) 
                table.insert(v, 16, true) 
                outputChatBox("Opened Gate!") 
            else 
                moveObject(theGate, a15, a3, a4, a5, a6, a7, 0) 
                table.remove(v, 16) 
                table.insert(v, 16, false) 
                outputChatBox("Closed Gate!") 
            end 
        end); 
    end 
end 
addEventHandler("onClientResourceStart", resourceRoot, createGates_Handler) 

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