Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a change team brick?

I'm trying to make a part, that when touched, changes the player's team. So far, I've done this:

script.Parent.Touched:Connect(function(hit)
	hit.Parent.Team = game.Teams:FindFirstChild("Criminal")
end)

I can't think of anything else I could do, and I've looked for other help on the internet. I would like it if someone was able to help.

1 answer

✓ Accepted answer

You're on the right track. Here's a few things you need to keep in mind:

The argument passed by the Touched event, hit, is the part that was touched. Therefore it would be part of the character, not the player. Team is a property of the player, not the character. Fortunately, you can use the GetPlayerFromCharacter() function to obtain the player object.

Secondly, it's possible that whatever hit your part was in fact not part of a player's character, so you want to check to make sure it is a character first. Here is what your script should look like when updated:

script.Parent.Touched:Connect(function(hit)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if player then --The code will only run if the player exists
		player.Team = game.Teams:FindFirstChild("Criminal")
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.