1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. GTA Network forum is now in archive mode.

[Vehicle Door] Open/close vehicle doors

Discussion in 'Scripts' started by mnzt, Dec 22, 2016.

  1. mnzt

    mnzt Member

    Messages:
    15
    Likes Received:
    5
    Joined:
    Dec 21, 2016
    [​IMG]

    Usage (Opening the hood):
    /open 4
    /close 4

    I may consider adding alias' in the future, so you can use /open hood, all, etc.

    Code:
    using System;
    using GTANetworkServer;
    
    namespace VehicleDoor
    {
        public class VehicleDoor : Script
        {
            //  0 = Front Left Door
            //  1 = Front Right Door
            //  2 = Back Left Door
            //  3 = Back Right Door
            //  4 = Hood
            //  5 = Trunk
            //  6 = Back
            //  7 = Back2
    
            public VehicleDoor()
            {
                API.onResourceStart += OnResourceStart;
            }
    
            public void OnResourceStart()
            {
                API.consoleOutput("[VehicleDoor] Starting");
            }
    
     
    [Command("open", GreedyArg = true)]
    public void OpenCommand(Client player, string door)
    {
        if (player.isInVehicle)
        {
           var doorId = Convert.ToInt32(door);
            player.vehicle.openDoor(doorId);
            API.sendChatMessageToPlayer(player, $"Opened door {door}");
            return;
        }
    
        API.sendChatMessageToPlayer(player, "You must be in a vehicle to open the door(s)");
        return;
    }
    
    
    [Command("close", GreedyArg = true)]
    public void CloseCommand(Client player, string door)
    {
        if (player.isInVehicle)
        {
            var doorId = Convert.ToInt32(door);
            player.vehicle.closeDoor(doorId);
            API.sendChatMessageToPlayer(player, $"Closed door {door}");
            return;
        }
    
        API.sendChatMessageToPlayer(player, "You must be in a vehicle to open the door(s)");
        return;
    }
        }
    }
    }
     
    Last edited: Apr 19, 2017
    Blitzer likes this.
  2. mnzt

    mnzt Member

    Messages:
    15
    Likes Received:
    5
    Joined:
    Dec 21, 2016
    CHANGELOG 001:
    - Confirmed working!
    - Updated code in line with public beta release
     
  3. Venican

    Venican Member

    Messages:
    10
    Likes Received:
    2
    Joined:
    Dec 30, 2016
    Heyo! You should probably check the seating position of the player before allowing them to open certain doors. If this is used for RP. Other than that, nice script! :=D
     
  4. mnzt

    mnzt Member

    Messages:
    15
    Likes Received:
    5
    Joined:
    Dec 21, 2016
    Hmm, that's pretty hardcore. However, it could be a nice addition.
     
  5. Venican

    Venican Member

    Messages:
    10
    Likes Received:
    2
    Joined:
    Dec 30, 2016
    It's actually pretty easy. Driver seat = -1 and it goes upwards from there. You would have to list two door vehicles and four door vehicles in their own lists tho. But if you ever have time it's something you should look into.
     
  6. Peterr

    Peterr New Member

    Messages:
    1
    Likes Received:
    1
    Joined:
    Apr 16, 2017
    I can't get this to work at all on my server. How do you get this working?

    EDIT: Never mind. I know nothing about coding and I managed to fix this. You had some kind of dependency that no one else uses that caused it to fail upon start. I removed it and fixed it. Now it works.

    Code:
    using System;
    using GTANetworkServer;
    
    namespace VehicleDoor
    {
        public class VehicleDoor : Script
        {
            //  0 = Front Left Door
            //  1 = Front Right Door
            //  2 = Back Left Door
            //  3 = Back Right Door
            //  4 = Hood
            //  5 = Trunk
            //  6 = Back
            //  7 = Back2
    
            public VehicleDoor()
            {
                API.onResourceStart += OnResourceStart;
            }
    
            public void OnResourceStart()
            {
                API.consoleOutput("[VehicleDoor] Starting");
            }
    
     
            [Command("open", GreedyArg = true)]
            public void OpenCommand(Client player, string door)
            {
                if (player.isInVehicle)
                {
                  var doorId = Convert.ToInt32(door);
                    player.vehicle.openDoor(doorId);
                    API.sendChatMessageToPlayer(player, $"Opened door {door}");
                    return;
                }
    
                API.sendChatMessageToPlayer(player, "You must be in a vehicle to open the door(s)");
                return;
            }
    
    
            [Command("close", GreedyArg = true)]
            public void CloseCommand(Client player, string door)
            {
                if (player.isInVehicle)
                {
                    var doorId = Convert.ToInt32(door);
                    player.vehicle.closeDoor(doorId);
                    API.sendChatMessageToPlayer(player, $"Closed door {door}");
                    return;
                }
    
                API.sendChatMessageToPlayer(player, "You must be in a vehicle to open the door(s)");
                return;
            }
        }
    }
    
     
    Last edited: Apr 17, 2017
    mnzt likes this.
  7. Mercenary

    Mercenary New Member

    Messages:
    2
    Likes Received:
    1
    Joined:
    Feb 17, 2017
    Why don't you just use "int door", instead of "string door" and then converting it to an int?
     
  8. enviel

    enviel Member

    Messages:
    23
    Likes Received:
    5
    Joined:
    Jan 26, 2017
    you should always use string on command args i think, so it doesn't give any error when players try to input an alphabet as argument
     
  9. -Andreas

    -Andreas Well-Known Member

    Messages:
    172
    Likes Received:
    25
    Joined:
    Sep 29, 2016
    I'm pretty sure that's not true. If the command is expecting an int, but you parse it a string it will simply tell you the command format instead.
     
  10. enviel

    enviel Member

    Messages:
    23
    Likes Received:
    5
    Joined:
    Jan 26, 2017
    have experience with that not long ago..yes the server will tell you the format but still throw an error
     

Share This Page