Trouble changing teams with part

you know what… if feels like we are over complicating this… why dont we just rewrite the code to do what we want it to. my original goal was:
there is a part with a script in it
if player touches the part it will check what team they are on.
if the player is a certian tema it will switch them to another team, if not nothing happens.
reason: i am making a game and in the game there is a prison with a spawnlocation in it that switches your team to prisoner and i want it to be that when the player exits the jail (or stops touching it) they become the criminal team

1 Like

i would just do that, but i am too inexperienced to scrit that, can you make a script that i descriped above this comment?

1 Like

@porcelian you were typing for like 10 minutes where did u go?

1 Like

hmm i tried this so far

part.Touched:connect(function(SwitchTeams)
	repeat until part.TouchEnded
	wait()
	end
	if SwitchTeams.Parent:FindFirstChild("Humanoid") then
		local PlayerTeam = game.Players:GetPlayerFromCharacter(SwitchTeams.Parent)
		if PlayerTeam.Team == game.Teams.red then
			PlayerTeam.Team = game.Teams.blue
		end
	end
end)
1 Like

dang bro @theseformasentence has been tryping for like 20 minutes

1 Like

is the part a region or a flat plate in the ground

1 Like

a flat plate on the ground that is a part

normally i never use touchended because it is a lot harder to manage, using just touched on barriers that change the team is a lot easier and more consistent

i would suggest going this route, try this instead:

local MaxDistance = 500 —this is a safety check to assure that players outside of the prison are moved to the criminal team (exploiters can tp across the wall)
local Players = {}
local teams = {}
teams.Criminal = game.Teams.CriminalTeam
teams.Prisoner = game.Teams.PrisonerTeam
teams.Casual = game.Teams.CasualTeam

local function onCharacterRemoving(character)
Players[game.Players:GetPlayerFromCharacter(character).UserId].TouchEvent = nil
end
local function onPlayerRemoving(player]
Players[player.UserId] = nil
end
local function onCharacterAdded(character)
local player = game.Players:GetPlayerFromCharacter(character)
Players[player.UserId].TouchEvent = character.Humanoid.Touched:Connect(function(part)
if part.Name == “PrisonerBarrier” then
if player.Team == teams.Prisoner then
player.Team = teams.Criminal
end
end
end)
end
local function onPlayerAdded(player)
Players[player.UserId] = player
player.CharacterAdded:Connect(onCharacterAdded(character))
player.CharacterRemoving:Connect(onCharacterRemoving(character))
end

game.Players.PlayerAdded:Connect(onPlayerAdded(player))
game.Players.PlayerRemoving:Connect(onPlayerRemoving(player))

while true do
for i,p in Players do
if p.Character and (p.Character:GetPivot().Position - Prison:GetPivot().Position).Magnitude > MaxDistance then
if p.Team == teams.Prisoner then
p.Team = teams.Criminal
end
end
end
task.wait(2)
end

—this script should work, the only difference is instead, you build invisible walls around your prison, naming then “PrisonerBarrier”. This has a safety check that insures players get the criminal team forced onto them

thank you for your time and effort. i hope this works

1 Like

im so sorry but the script did not work and i amde the invisible barriers and stuff, maybe just to be sure, can you test it yourself?

TeamChange.rbxl (48.2 KB)


Try this…

You start as civilian
If you go to orange cube, you are a prisoner, but only if you are not a police (blue cube)
If you are prisoner and leave the green cube, you will be criminal, but only if you are prisoner.

Here is the code for the green cube.

local TouchList = {}

function SwitchTeamCriminal(player)
	if player.Team == game.Teams.Prisoner then
		player.Team = game.Teams.Criminal
	end
end

function SwitchTeamPrisoner(player)
	if player.Team == game.Teams.Criminal then
		player.Team = game.Teams.Prisoner
	end
end

script.Parent.Touched:Connect(function(part)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	if player and part.Name == "HumanoidRootPart" and not TouchList[player.UserId] then
		TouchList[player.UserId] = player.UserId
		SwitchTeamPrisoner(player)
	end
end)

script.Parent.TouchEnded:Connect(function(part)
	local player = game.Players:GetPlayerFromCharacter(part.Parent)
	if player and part.Name == "HumanoidRootPart" and TouchList[player.UserId] then
		TouchList[player.UserId] = nil
		SwitchTeamCriminal(player)
	end
end)


i changed my original code a little, maybe we can just fix this instead?

part.Touched:connect(function(SwitchTeams)
	repeat until part.TouchEnded
	wait()
	end
	if SwitchTeams.Parent:FindFirstChild("Humanoid") then
		local PlayerTeam = game.Players:GetPlayerFromCharacter(SwitchTeams.Parent)
		if PlayerTeam.Team == game.Teams.red then
			PlayerTeam.Team = game.Teams.blue
		end
	end
end)

bro that was the original idea kinda there were civilians, prisoners, police, criminals, and thats all

i might as well make it to where if a prisoner touches the baseplate they become criminal

i’m having trouble understanding why the original script doesn’t work change change the part to an invisible region and it should work

Try the download I made, and see if my code works good for your needs.
Its really all about limiting the touched and touchended to the humanoid, and keeping track of that.

Wait, what do you think is the issue?

@Fimutsuu what is a invisible region?
and @SelDraken i will try that but can you send me the download?

Try searching up in here: Change Team Script?

i think it’s the fact that he has a huge baseplate and he just checks for TouchEnded on it thinking it will only fire when you leave it’s boundaries