alienwave

mp.players.local.vehicle.getSpeed() returns me error

Recommended Posts

let vehicle = mp.players.local.vehicle;

let counter = {
	init() {
	let speed = vehicle.getSpeed() * 3.6;
	timerBars.speedBar.text = `${speed}`;
	}
}

function playerEnterVehicleHandler() {
	timerBars.show(true);
	
	counter.init();
}

mp.events.add('playerEnterVehicle', playerEnterVehicleHandler);

I write this code, but it returns me "Cannot read property getSpeed()", what should i do?

Edited by alienwave

Share this post


Link to post
Share on other sites

mp.players.local.vehicle doesn't exist until the player is actually sitting down on a seat, that's why you get the error because the event has tried running while you're still trying to sit down. Code is fine, you just can't use just the playerEnterVehicle event, you also need to check if they're in a seat as well before running counter.init()

Edited by MrPancakers

Share this post


Link to post
Share on other sites
6 hours ago, MrPancakers said:

mp.players.local.vehicle doesn't exist until the player is actually sitting down on a seat, that's why you get the error because the event has tried running while you're still trying to sit down. Code is fine, you just can't use just the playerEnterVehicle event, you also need to check if they're in a seat as well before running counter.init()

okey, thank you!

Share this post


Link to post
Share on other sites

Use render event like this 

mp.events.add('render', () =>
{
if (player.vehicle) {

//whatever you want

}

});

 

Hope it helped

Share this post


Link to post
Share on other sites
22 hours ago, Tecidis said:

Use render event like this 

mp.events.add('render', () =>
{
if (player.vehicle) {

//whatever you want

}

});

 

Hope it helped

lol, thank you, it works. "mp.players.local.vehicle" returns me error, but player.vehicle work fine(

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.