yusa

Stop Function after Player disconnets

Recommended Posts

Hey Guys,

hope you can help me with my problem.

i want to end a function in a Event, if the player disconnects or leave the Server. But i only get crashes.

Here my Code:


function playerQuit(player) {
	console.log(`${player.name} has quit.`);
}
mp.events.add("playerQuit", playerQuit);



// After player die
mp.events.add("playerDeath", (player, reason, killer) => {
	//Set values hospital -> player dead/alive
	//hospitaltime -> time that he have to be in hospital
	player.hospital = 1;
	player.hospitaltime = 12;
	//update values in database
	gm.mysql.handle.query('UPDATE `accounts` SET hospital = ?, hospitaltime = ? WHERE username = ?', [player.hospital, player.hospitaltime, player.name], function(err, res, row){
		if(err) console.log(err);
	});

	if(player){
		gm.mysql.handle.query('SELECT hospital, hospitaltime FROM `accounts` WHERE username = ?', [player.name], function(err, res){
			if(err) console.log(err);
			//Read Values from Database
			player.hospital = parseInt(res[0]["hospital"]);
			player.hospitaltime = parseInt(res[0]["hospitaltime"]);
		});


		//if player is dead do ->
		if(player.hospital == 1){
			var timeLeft = parseInt(player.hospitaltime);
			var timerID = setInterval(countdown, 5000);

			//Countdown for hospitaltime
			function countdown(){
				//if time over player have to be spawned and timeout have to be cleared
				if(timeLeft == 1){
					clearTimeout(timerID);
					player.hospitaltime = 0;
					player.hospital = 0;

					gm.mysql.handle.query('UPDATE `accounts` SET hospital = ?, hospitaltime = ? WHERE username = ?', [player.hospital , player.hospitaltime, player.name], function(err, res, row){
						if(err) console.log(err);
					});
					player.spawn(new mp.Vector3(-799.5113525390625, -99.25785827636719, 37.604530334472656));
					return;
				} else {

					// if player leave put values in database
					if(playerQuit.player.name == player.name){
						clearTimeout(timerID);
						player.hospital = 1;
						player.hospitaltime = timeLeft;
						gm.mysql.handle.query('UPDATE `accounts` SET hospital = ?, hospitaltime = ? WHERE username = ?', [player.hospital , player.hospitaltime, player.name], function(err, res, row){
							if(err) console.log(err);
						});
						return;
					}
					
					//write new values
					timeLeft--;
					gm.mysql.handle.query('UPDATE `accounts` SET hospitaltime = ? WHERE username = ?', [timeLeft, player.name], function(err, res, row){
						if(err) console.log(err);
					});
				}
				
			}
			
		}
	}
});

 

to test this code i have in my cmd.js this:

 

mp.events.addCommand('kick', (player, target) => {
	let newTarget = mp.players.at(target);
	if(!target || isNaN(target)) return player.outputChatBox("Syntax: /kick [playerID]");
	if(newTarget === null) return player.outputChatBox("There is no player online with the ID given.")
	newTarget.outputChatBox("You have been kicked from the server.");
	newTarget.kick('Kicked.');
});

My server crashes everytime when i kick me, when im in the hospital. it also crashes without this Code section:

					// if player leave put values in database
					if(playerQuit.player.name == player.name){
						clearTimeout(timerID);
						player.hospital = 1;
						player.hospitaltime = timeLeft;
						gm.mysql.handle.query('UPDATE `accounts` SET hospital = ?, hospitaltime = ? WHERE username = ?', [player.hospital , player.hospitaltime, player.name], function(err, res, row){
							if(err) console.log(err);
						});
						return;
					}

 

i get following logs in my console:

 

Error: asyc stack has become corrutep (actual: 29, expected: 30)
1: 00007FF824079805
2: 00007FF824059B82
3: 00007FF824069CB3
4: 00007FF82409ABD0
5: 00007FF82415BCDE
6: 00007FF8241F810D
7: 00007FF8241F5066
[...]***more of them***

 

 

Edit:

 

The problem was, that i had 2 events with the same name. now it does not crash anymore

Edited by yusa

Share this post


Link to post
Share on other sites

You should also always use try and catch inside timer functions, otherwise the async stack can become corrupted and the server will crash.

  • Like 1

Share this post


Link to post
Share on other sites
vor 2 Stunden schrieb LeonMrBonnie:

You should also always use try and catch inside timer functions, otherwise the async stack can become corrupted and the server will crash.

Yeah, youre right. Thank you!

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.