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
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
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
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.
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)
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)
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
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