tornado711 Posted May 9, 2016 Share Posted May 9, 2016 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 More sharing options...
Jitnaught Posted May 9, 2016 Share Posted May 9, 2016 (edited) 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 May 9, 2016 by Jitnaught tornado711 1 Link to comment Share on other sites More sharing options...
tornado711 Posted May 9, 2016 Author Share Posted May 9, 2016 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 More sharing options...
Jitnaught Posted May 9, 2016 Share Posted May 9, 2016 (edited) 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 May 9, 2016 by Jitnaught Link to comment Share on other sites More sharing options...
tornado711 Posted May 9, 2016 Author Share Posted May 9, 2016 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 More sharing options...
Jitnaught Posted May 9, 2016 Share Posted May 9, 2016 It's fixed now in your updated code. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now