Scripting, player change team when in an area

I am trying to make it so that when a player enters an area they switch teams.

I’m not sure if the best option for my game is by adding invisible non collide parts to each area and then making it so that when it is touched the player switches team.

I have already tried this and searched for quite a few tutorials but none of them have worked for me. Any tutorials etc will be much appreciated.

2 Likes

You could try putting ivisible spawns on the floor spawns have team change option. I don’t know wcih situation is.

2 Likes

Put a script into the part that changes their team.

Paste this inside:

--//Services
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

--//Variables
local Part = script.Parent

--//Controls
local Team = Teams:WaitForChild("MyCoolTeamName")

--//Functions
Part.Touched:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	
	--//Checks if there is a player
	if Player then
		
		--//Sets the player's team
		Player.Team = Team
	end
end)

This works for me, so if any errors appear please tell me.

2 Likes

This won’t work for my game disappointingly. This is because of the map. When you move to a different area you don’t spawn in it you just walk into it.

1 Like

You can set spawn point to to neutral.

1 Like

By the way I have tried something similar to this. It comes up with team is not a valid member of this player.

Sorry wrong reply I was meant to reply to Katrist

1 Like

Tomorrow evening I will check this out and look further into solving this problem and show more about my game.

1 Like

Hm, could you paste the actual error? It works fine for me.

1 Like

Just tested it again and it seems to work fine. Are you sure you’re putting in the right team name?

So you want the team change, but you don’t want them to spawn on the team spawn? Is that what you mean?

I will check to see if it works tomorrow evening. Thanks for the help I really appreciate it.

Herbz I will explain it tomorrow evening as well.

Please forgive me for posting this topic, I should have done it tomorrow when I am free to develop.

Hi there! Thanks for the help!

Screen Shot 2022-08-31 at 8.13.57 pm
Screen Shot 2022-08-31 at 8.14.26 pm

Error: Room is not a valid member of Player “Players.cobaysaurus_rex” - Server - Script:20
The same error keeps happening in every video I watch. cobaysaurus_rex is my username just so you know.


--//Services

local Players = game:GetService("Players")

local Rooms = game:GetService("Teams")

--//Variables

local TheatreHitbox = script.Parent

--//Controls

local Room = Rooms:WaitForChild("Lobby")

--//Functions

TheatreHitbox.Touched:Connect(function(hit)

local Player = Players:GetPlayerFromCharacter(hit.Parent)

--//Checks if there is a player

if Player then

--//Sets the player's team

Player.Room = game.Rooms.Theatre

end

end)

I have decided I want the team to change when a player touches a part because I think thats the best way to go about it.

Thank you so much for helping me. I figured out what was wrong with my script, which is that Players.Room should have been kept as Players.Team. :smiley: It works like a charm!!! I always learn quite a bit from these experiences.