If a player is on a certain team and touches a brick, they switch teams

Here’s what I have so far, it’s meant so if a Inmate touches a brick, they are sent to the Criminals team, but only for Inmates so Guards will be prevented from becoming Criminals also. It seem as if the if statement for checking if they are a inmate doesn’t work correctly.

tm = script.Parent

function onTouched(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
local n = hit.Parent
		local p = game.Players:FindFirstChild(""..n.Name.."")
		if p.Team == "Inmates" then
if p ~= nil then
p.TeamColor = tm.BrickColor
end
end
	end
	end

script.Parent.Touched:connect(onTouched)
3 Likes

If you do this, you’re looking for “n.name”. The name of an instance is already a string so there’s no need to add extra quotation marks.

local p = game.Players:FindFirstChild(n.Name)

However, there’s a way to get the player directly from the player. You could use this instead of you wanted to:

local p = game.Players:GetPlayerFromCharacter(n)

[EDIT]

“”…n.Name…“” This is basically a string that consists of “…n.Name…”. As you may have guessed, there probably isn’t going to be a player in Players that’s named “…n.Name…”

2 Likes

I’m pretty sure in this case you wouldn’t even need a script, all you would have to do is replace the part with spawn, and set the team color to the color of the team(all teams can be associated with the color of the team that’s why its not recommended to have teams have the same team color), then enable AllowTeamChangeOnTouch, hopefully, this solves your problem.

https://developer.roblox.com/en-us/api-reference/class/SpawnLocation


If you want you can also disable it from having players spawn there.

Well I don’t intend for the part itself to become a spawn point for the criminals

I just updated the reply, you can disable the property for a player to spawn there. Hopfully this helps too.

Your script should be :

tm = script.Parent

function onTouched(hit)
local h = hit.Parent:FindFirstChild(“Humanoid”)
if h ~= nil then
local n = hit.Parent
local p = game.Players:FindFirstChild(n.Name)
if p.Team.Name == “Inmates” then
if p ~= nil then
p.Team.Name = “[Team Name Here]”
end
end
end
end

script.Parent.Touched:connect(onTouched)

Wouldn’t this literally change the player’s team’s name? You would have to do p.Team.TeamColor, or p.Team.

Oops, thx for telling me. It been a while since I messed with Teams related stuff in studio, so i forget a few things about it.

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)

Sorry for late reply, but after testing i found out that when touching the brick, the game gets laggy, do you know the source of this?