Jump to content

Gate open/close


Recommended Posts

Hey,

I'm trying to script a gate, which moves, when a Player types /gate1. The player has to be in a specific range to a certain point, so that he can't open the gate, whereever he is. The gate spawns, but I can't open it. It doesn't happen anything at all. The script is client side. I tried it with different types of targets (such as localPlayer, getLocalPlayer(), source, root,...). Please help :)

Also I wanted to make a switch, so there's 1 command for open AND closing the gate. I began with a boolean (gate1_open). I didn't know how to start. I'm a very beginner! I'm a bit confused about client and serverside (with source or localPlayer or whatever) :D

  
gate1 = createObject ( 975, -2695.5, 1274.8000488281 , 56.099998474121, 0, 0, 180) 
gate1_open = false 
function gate_func() 
    function openGate1()  
    if (isElementInRange(getLocalPlayer(), -2690.70654, 1268.74756, 55.94558, 2) == true) then 
        moveObject(gate1, 4000, -2695.5, 1274.8000488281, 52.499998474121) 
        if (gate1_open == false) then 
            gate1_open = true 
        else 
            gate1_open = false 
        end 
    else 
        outputChatBox("Stelle Dich in den Wirkungsbereich!", getLocalPlayer(), 255, 0, 0, false) 
    end 
    end 
end 
addEventHandler("onResourceStart", getRootElement(), gate_func) 
addCommandHandler("gate1", openGate1) 
  

Link to comment

Very first of all, second argument of `outputChatBox` in client-sided shouldn't be a player, just the color codes, and your fifth argument isn't needed since you added no hex color codes into your sentence.

Secondly have you declared the 'isElementInRange' function? If that's all your code, it's invalid as well. Third, you don't have to use both 'onResourceStart' and the command handler at the same function literally. If you use command handler only, it would be called when you write the /gate1 even if there's no onResourceStart. If this isn't all your code, try to post it all

Link to comment

Hi,

unfortuately, this is all my code. I don't know how to declare 'isElementInRange'. How do I do that? I thought it would be done by typing getLocalPlayer(). I guess I fixed the other issues.

  
gate1 = createObject ( 975, -2695.5, 1274.8000488281 , 56.099998474121, 0, 0, 180) 
gate1_open = false 
function gate_func() 
    function openGate1()  
    if (isElementInRange(getLocalPlayer(), -2690.70654, 1268.74756, 55.94558, 2) == true) then 
        moveObject(gate1, 4000, -2695.5, 1274.8000488281, 52.499998474121) 
        if (gate1_open == false) then 
            gate1_open = true 
        else 
            gate1_open = false 
        end 
    else 
        outputChatBox("Stelle Dich in den Wirkungsbereich!", 255, 0, 0) 
    end 
    end 
end 
addCommandHandler("gate1", openGate1) 
  

Link to comment

If you want to use isElementInRange then you have to include this somewhere in your code:

  
function isElementInRange(ele, x, y, z, range) 
   if isElement(ele) and type(x) == "number" and type(y) == "number" and type(z) == "number" and type(range) == "number" then 
      return getDistanceBetweenPoints3D(x, y, z, getElementPosition(ele)) <= range -- returns true if it the range of the element to the main point is smaller than (or as big as) the maximum range. 
   end 
   return false 
end 
  

I'll re-edit this code in a bit, gonna edit this post later

EDIT:

  
gate1 = createObject (975, -2685.5, 1274.8, 56.1, 0, 0, 180) 
gate1_open = false 
  
--You have to include this 
function isElementInRange(ele, x, y, z, range) 
   if isElement(ele) and type(x) == "number" and type(y) == "number" and type(z) == "number" and type(range) == "number" then 
      return getDistanceBetweenPoints3D(x, y, z, getElementPosition(ele)) <= range -- returns true if it the range of the element to the main point is smaller than (or as big as) the maximum range. 
   end 
   return false 
end 
  
addCommandHandler ("gate1", 
    function () 
        if (isElementInRange (localPlayer(), -2690.7, 1268.7, 55.95, 2) == true) then --Dunno if this works, I always did the range-check manually 
            moveObject (gate1, 4000, -2695.5, 1274.8, 52.5) 
            if (gate1_open == false) then --?! 
                gate1_open = true--?! 
            else--?! 
                gate1_open = false--?! 
            end 
        else 
            outputChatBox ("Stelle dich in den Wirk...", 255, 0, 0) 
        end 
    end 
) 
  
  

the --?! I wrote them because I don't know why you wrote those, if you want to do something later with them then fine, but if this all you want they're useless

Link to comment

So if u want to learn to script use:

https://wiki.multitheftauto.com/wiki/Main_Page U can learn alot of stuff from wiki and is useful af

https://www.lua.org/ Basic lua language(IMPORTANT)

After learning lua try this:

https://wiki.multitheftauto.com/wiki/Scripting_Introduction

Then

Try to start with basic scripts(showing a message etc..)

After that try to make ur scripts more advanced

GUI is very inportant in MTA

https://wiki.multitheftauto.com/wiki/Introduction_to_Scripting_the_GUI

And wiki functions has alot of examples,look at these too

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