if p.Team == "Inmates" then
This is the issue. p.Team
is a Team object, not the name of a team.
The quick fix would be:
if p.Team == game.Teams.Inmates then
This should make your script work without any other changes.
Of course, the code is extremely low-quality, so bear with me while I point out how you can do better:
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
You can make the code less pyramid-like by making this a “sentinel” instead:
local h = hit.Parent:FindFirstChild("Humanoid")
if h == nil then return end -- if there is no humanoid, then stop right here
local p = game.Players:FindFirstChild(""..n.Name.."")
Avoid finding a player by looking for the character’s name in Players. The character’s name might be changed, or something that isn’t a player could be in Players.
Instead, use the function that’s meant for this:
local p = game.Players:GetPlayerFromCharacter(n) -- n is the character model
As a bonus, this lets you just delete the sentinel above, because if the model is a player’s character, then it must also have a Humanoid, so it makes no sense to check for it separately.
if p.Team == game.Teams.Inmates then
if p ~= nil then
The if p ~= nil then
is redundant. If p’s Team is the one we’re looking for, then p must also exist. This check can be deleted completely and change nothing.
p.TeamColor = tm.BrickColor
This makes it so the part must be the color of the Criminal team. If either color changes, e.g. you recolor the spawn to disguise it, it will stop working right.
It isn’t that bad. This works really well as long as the color stays right.
game.Players
Services (like Players) should usually be gotten with `game:GetService(“Players”), but it’s not that important.
Resulting code:
local color = script.Parent.BrickColor
local teamInmates = game.Teams.Inmates
-- local teamCriminals = game.Teams.Criminals
function onTouched(hit)
local p = game.Players:GetPlayerFromCharacter(hit.Parent)
if p.Team == teamInmates then
p.TeamColor = color
-- p.Team = teamCriminals
end
end
script.Parent.Touched:connect(onTouched)