How to disable team change when touch another team spawn?

aac3
Joined 31/12/2020
Posts 8
09:09 AM 05/03/2023
I'm want disable team change when you touch another team spawn. How to do that?


KARLPIEM
Joined 24/02/2023
Posts 365
09:21 AM 05/03/2023
i have no clue


[elite] Joe Elite
Joined 25/02/2023
Posts 2,606
01:41 PM 07/03/2023
Refer to first reply


[Glith] HolisReborn
Joined 04/05/2021
Posts 930
09:07 PM 08/03/2023
Create a boolean variable to keep track of whether a player is allowed to change teams.

let allowTeamChange = true;
Add an event listener to detect when a player touches a spawn point:

game.workspace.spawnPoint.Touched.connect(function(part) {
if (part.Name == "RedSpawn" && player.TeamColor == "Blue") {
allowTeamChange = false;
// Display a message to the player informing them that they can't change teams
} else if (part.Name == "BlueSpawn" && player.TeamColor == "Red") {
allowTeamChange = false;
// Display a message to the player informing them that they can't change teams
} else {
allowTeamChange = true;
}
});

Modify the code that changes the player's team to check the allowTeamChange variable:

if (allowTeamChange) {
player.TeamColor = "Red";
}


[RWB] MixaMega
Joined 23/12/2019
Posts 435
Moderator
12:01 AM 09/03/2023
spawn bricks have a touch interval to do this, so if you run _cleanup() for each spawn brick it should work

world.spawns.forEach(brick=>brick._cleanup())


dargy
Joined 23/06/2019
Posts 4,820
12:12 AM 09/03/2023
Quote from MixaMega , 12:01 AM 09/03/2023
spawn bricks have a touch interval to do this, so if you run _cleanup() for each spawn brick it should work

world.spawns.forEach(brick=>brick._cleanup())
Adding onto MixaMega's wonderful reply, let me explain more of what the _cleanup() function does to a brick.

A general breakdown of what it does is as follows:
1. Clears the "_hitMonitor" interval, which is a monitor to check if a player is touching a brick. This is what powers the .on touching listener.
2. It calls the function "removeAllListeners" for the brick. Pretty self-explanatory. Any .on listeners for the brick are destroyed
3. For each brick.setInterval that is set, it clears that too.

Ultimately, you end up with a brick with no event listeners or intervals. A perfectly clean brick. Hope this helps!

1