So after a full day of trying to understand why this doesn't works and discussing the problem on discord and still didn't managed to fix it i decided to post here on forum, maybe someone that has already been here with this issue found a fix. I also want to thank George and the other people from discord that tried to help me fix this.
So here is the issue: I'm trying to call player.TaskEnterVehicle but with no success, I made sure the vehicle is not null, I made sure the vehicle is close to the player, still not working. I tried the exact same code but with a different task (it was something with exit vehicle) and it worked just fine. But for some reason the TaskEnterVehicle doesn't wants to.
public class FGVehicleEnter : RAGE.Events.Script
{
private bool lastFstatus = false;
private bool lastGstatus = false;
public FGVehicleEnter()
{
RAGE.Events.Tick += Event_Key;
}
private void Event_Key(List<RAGE.Events.TickNametagData> nametags)
{
RAGE.Game.Pad.DisableControlAction(0, 23, true);
// Check if Key is pressed and if Cursor is not active(so it won't trigger when chat is open)
if (RAGE.Input.IsDown(0x47) && !RAGE.Ui.Cursor.Visible)
{
if (!lastGstatus)
{
lastGstatus = true;
RAGE.Chat.Output("Pressed G");
Player player = Player.LocalPlayer;
Vehicle closestVehicle = GetClosestVehicle(player.Position);
if (closestVehicle != null && closestVehicle.Position.DistanceTo(player.Position) < 5 && closestVehicle.AreAnySeatsFree())
{
Player.LocalPlayer.TaskEnterVehicle(closestVehicle.Handle, 5000, 0, 2.0f, 1, 0); // Problem: not entering vehicle
RAGE.Chat.Output("Seat free"); // Gets printed to the chat, so that means the above line doesn't throws any error?
}
}
}
else
lastGstatus = false;
}
public static Vehicle GetClosestVehicle(RAGE.Vector3 position)
{
List<Vehicle> allVehicles = RAGE.Elements.Entities.Vehicles.All;
Vehicle closestVehicle = null;
foreach(Vehicle vehicle in allVehicles)
{
if (closestVehicle == null) closestVehicle = vehicle;
else if (position.DistanceTo(vehicle.Position) < position.DistanceTo(closestVehicle.Position)) closestVehicle = vehicle;
}
return closestVehicle;
}
}
So when I press F and there is no nearby car it just says "Pressed F", as it should. when I'm near a car, it also says "Seat free" as it should, but the line above that (the task) does nothing. It's my C# implementation of this resource: https://rage.mp/files/file/74-vehicle-fg-keys-entering/
I want to implement it on my own because I want to keep the server C# only, and do not mix it with JS resources and also I want to add some more custom functionality to it (for server admins) so using that resource isn't really a fix for me.
Other things I tried (still not working):
Player.LocalPlayer.TaskEnterVehicle(closestVehicle.Handle, 5000, 0, 2, 1, 0); // Speed as int instead of float (let the function handle the conversion)
Player.LocalPlayer.TaskEnterVehicle(closestVehicle.Handle, 5000, 0, 2, 1534654, 0); // Random int as p5
Player.LocalPlayer.TaskEnterVehicle(closestVehicle.Handle, 5000, 0, 1, 1, 0); // Speed 1 (Walk) instead of 2 (Run)
Player.LocalPlayer.TaskEnterVehicle(closestVehicle.Handle, 5000, 0, 2, 3, 0); // Teleport the player to the vehicle (p5 = 3)
// I also tried with different seat numbers, still nothing
// I tried with different p6 (last arg), wiki says it should be 0 always tho, not working with 0, not working with any other number