Trouble changing teams with part

so lemme give yall some backround:
im working on a game and if you gget put in the prison you become prisoner team. i want to make it to where if you leave the prison you become prisoner team. in the script below im testing in a test game so the team names are different, the red is prisoner and the blue is criminal, but its not working, can someone please help me?

part.TouchEnded:connect(function(SwitchTeams)
	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)

it is kinda working it just switches teams when you touch it instead of when you stop touching it

11 Likes

If you add a third team, like “casuals”, in between, you can check if the player touching it is on the casuals team. If they are, return, if they are instead on the “Prisoners” team, change their team to “Criminals”. you can view it like this:

local teams = {}
teams.Casual = game.Teams.CasualTeam
teams. Prisoner = game.Teams.PrisonerTeam
teams.Criminal = game.Teams.CriminalTeam

local PrisonBox = workspace.PrisonTrigger
PrisonBox.TouchEnded:Connect(function(part)
local player_ = game.Players:GetPlayerFromCharacter(part.Parent)
if player_ == nil then return end
if player.Team == teams.Prisoner then
player.Team = teams.Criminal
end
end

10 Likes

wouldnt it be easier just to make that team the prisoners team, the causals team seems unessicary because it would change up the player leaderboard

8 Likes

would it still work if i changed the prisonbox variable to a different name?

7 Likes

It would. you can really skip that variable since it is only used once, and cut straight to workspace.PrisonTrigger.TouchEnded:Connect()

6 Likes

um im sorry no offense but i dont find your solution a very good solution, for it does not work. in the teams folder i have 3 teams: “Criminal” “Prisoner” “Casuals” and “Casuals” is the only austo assinghable one.
here is the script that does not work.

teams.Casual = game.Teams.CasualTeam
teams. Prisoner = game.Teams.PrisonerTeam
teams.Criminal = game.Teams.CriminalTeam

local PrisonBox = workspace.PrisonTrigger
PrisonBox.TouchEnded:Connect(function(part)
	local player_ = game.Players:GetPlayerFromCharacter(part.Parent)
	if player_ == nil then return end
	if player.Team == teams.Prisoner then
		player.Team = teams.Criminal
	end
end
5 Likes

was there an error thrown anywhere?

5 Likes

image

4 Likes

does that answer you question? (there is no error that i know of)

6 Likes

Is there a variable for “teams”?

5 Likes

I think my solution gets the same issue as the original code, but the only other way I can see how to fix it is to check every second or so if the player is still inside the part.

5 Likes

i think there is team variable but this code was given too me from another topic in devforum

5 Likes

the original problem is that it runs when you touch the part not when you stop touching it like its supposed too

5 Likes

This could work i guess.

part.Touched:Connect(function(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChild("Humanoid")
    
    if humanoid and character:IsA("Model") then
        local player = game.Players:GetPlayerFromCharacter(character)
        
        if player and player.Team == game.Teams.red then
            player.Team = game.Teams.blue
        end
    end
end)
4 Likes

im afriad it didnt work…

part.Touched:Connect(function(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChild("Humanoid")
    
    if humanoid and character:IsA("Model") then
        local player = game.Players:GetPlayerFromCharacter(character)
        
        if player and player.Team == game.Teams.red then
            player.Team = game.Teams.blue
        end
    end
end)
2 Likes

Oh sorry my bad. I accidentaly changed the event “TouchEnded” into “Touched” try remaking the first line and see if it helped.

part.TouchEnded:Connect(function(otherPart)
1 Like

The bigger problem here is more of understanding what happens. If you create a new script, and create two connections between TouchEnded and Touched onto a CanCollide false brick then print in each side, you will see that you fire both touched and touchended maybe up to 15x each, this is because the touched and touchended event are accounting for every single part in the character rig

1 Like

ok… using this knowledge, can you make me a script

1 Like

okay :+1:

try:

local players = {}

local PrisonBox = workspace.PrisonTrigger
local function IsPlayerInBox(player)
local query = workspace:GetPartsInPart(PrisonBox)
for _, c in query do
if c.Name = “Head” and game.PlayersGetPlayerFromCharacter(c.Parent) then
players[player.UserId].Jailed = true
else
players[player.UserId].Criminal = if players[player.UserId].Jailed then true else false
players[player.UserId].Jailed = false
end
end
end

game.Players.PlayerAdded:Connect(function(player)
players[player.UserId]
players[player.UserId].Jailed = false
players[player.UserId].Criminal = false
player.CharacterAdded:Connect(function(character)
while player.Character do
if players[player.UserId].Criminal then
player.Team = teams.CriminalTeam
else
player.Team = teams.PrisonerTeam
end
IsPlayerInBox(player)
task.wait(1)
end
end)
end)

now this whole this is assuming you have only two teams and that you cannot be freed from jail (there are definitely much better ways to do this)
you also cannot become uncriminal

1 Like

um… if it assumes there are only two teams then it should not work… are there any changes you can make to where there can be more than 2 teams?

1 Like