Jump to content
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. Los Santos Drug Wars
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

    3. Suggestions

How to spawn a car at your position only while in car?


tornado711
 Share

Recommended Posts

tornado711

Below is a simple script I wrote up that will only allow the player to spawn a car at his position while he is in a vehicle. However, it does not seem to work. I open gta v and load my DotNet scripts however, upon pressing the assigned keys, nothing happens. Can anyone see what I'm doing wrong in my code?

 

public class Class1 : Script
{
bool CarCheck = Game.Player.Character.IsInVehicle();
public void Main()
{
KeyUp += Keywal;
}
public void Keywal(object sender, KeyEventArgs g)
{
if (CarCheck == true && g.KeyCode == Keys.G)
{
World.CreateVehicle(VehicleHash.Kuruma2, Game.Player.Character.CurrentVehicle.Position);
}
}
}
}
Link to comment
Share on other sites

Jitnaught

Remove the CarCheck variable entirely and check to see if Game.Player.Character.IsInVehicle() is true within the if statement.

public class Class1 : Script{   public void Main()   {      KeyUp += Keywal;   }    public void Keywal(object sender, KeyEventArgs g)   {      if (Game.Player.Character.IsInVehicle() && g.KeyCode == Keys.G) //just putting "Game.Player.Character.IsInVehicle()" is the same thing as "Game.Player.Character.IsInVehicle() == true"      {         World.CreateVehicle(VehicleHash.Kuruma2, Game.Player.Character.CurrentVehicle.Position);      }   }}

The reason that happens is because since you call that function that checks if the player is in a vehicle outside of the KeyUp event function, it only gets checked once: at the start of the game.

 

Also I suggest rearranging the if statement so that the key is checked first, then check if you're in a vehicle. With the current if statement it checks if you are in a vehicle when you press ANY button. That means if you are just driving along, only pressing WASD, it will checking if you are in a vehicle constantly, which doesn't need to happen.

Edited by Jitnaught
Link to comment
Share on other sites

tornado711

Remove the CarCheck variable entirely and check to see if Game.Player.Character.IsInVehicle() is true within the if statement.

public class Class1 : Script{   public void Main()   {      KeyUp += Keywal;   }    public void Keywal(object sender, KeyEventArgs g)   {      if (Game.Player.Character.IsInVehicle() && g.KeyCode == Keys.G) //just putting "Game.Player.Character.IsInVehicle()" is the same thing as "Game.Player.Character.IsInVehicle() == true"      {         World.CreateVehicle(VehicleHash.Kuruma2, Game.Player.Character.CurrentVehicle.Position);      }   }}

The reason that happens is because since you call that function that checks if the player is in a vehicle outside of the KeyDown event function, it only gets checked once: at the start of the game.

 

Also I suggest rearranging the if statement so that the key is checked first, then check if you're in a vehicle. With the current if statement it checks if you are in a vehicle when you press ANY button. That means if you are just driving along, only pressing WASD, it will checking if you are in a vehicle constantly, which doesn't need to happen.

Thanks for pointing that out to me! I actually rearranged the two if statement conditions and now the mod works check it out below! Also, so what you're saying is since I used the key up function, essentially, it's checking it only once, right as the game starts?

public class Firstmod : Script
{
public Firstmod()
{
KeyUp += Keywal;
}
public void Keywal(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.O && Game.Player.Character.IsInVehicle())
{
World.CreateVehicle(VehicleHash.Buffalo, Game.Player.Character.Position);
Game.Player.Character.Armor = 15;
}
}
}
}
Link to comment
Share on other sites

Jitnaught

Also, so what you're saying is since I used the key up function, essentially, it's checking it only once, right as the game starts?

No, what I'm saying is that since you put the piece of code that checks if you are in the vehicle outside of the KeyUp function, it only checks it once.

 

P.S. If you want to format your code a little better, press the little double-bracket button and then put the code in the form that pops up.

Edited by Jitnaught
Link to comment
Share on other sites

tornado711

 

Also, so what you're saying is since I used the key up function, essentially, it's checking it only once, right as the game starts?

No, what I'm saying is that since you put the piece of code that checks if you are in the vehicle outside of the KeyUp function, it only checks it once.

 

P.S. If you want to format your code a little better, press the little double-bracket button and then put the code in the form that pops up.

 

Thank you for telling me this. Could you show me how I could correct this error only regarding checking it once. Sorry for the simple questions I'm fairly new to this haha :(

Link to comment
Share on other sites

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
 Share

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.