Team Changer Issue

local Teams = game:GetService("Teams")

local CivilianJoinButton  = script.Parent.Parent.Jobs.Civilian.CivilianJoin
local PoliceJoinButton  = script.Parent.Parent.Jobs.Police.PoliceJoin
local FirefighterJoinButton  = script.Parent.Parent.Jobs.Firefighter.FirefighterJoin
local DepartmentOfTransportationJoinButton  = script.Parent.Parent.Jobs.DepartmentOfTransportation.DepartmentOfTransportationJoin

CivilianJoinButton.MouseButton1Click:Connect(function(Player)
	Player.Team = Teams.CivilianTeam
end)

PoliceJoinButton.MouseButton1Click:Connect(function(Player)
	Player.Team = Teams.PoliceTeam
end)

FirefighterJoinButton.MouseButton1Click:Connect(function(Player)
	Player.Team = Teams.FirefighterTeam
end)

DepartmentOfTransportationJoinButton.MouseButton1Click:Connect(function(Player)
	Player.Team = Teams.DepartmentOfTransportationTeam
end)

It keeps saying Team is nil. What’s the issue. I got to go soon so I’ll check responses later.

Correct me if I’m wrong but I believe you switch teams using the TeamColor method.

plr.TeamColor = Teams.CivilianTeam.TeamColor

You can change the team using Player.Team = Teams.FirefighterTeam.

Oh, I see, I have barely any experience using Teams since I personally just create my own Team systems so thanks for the enlightenment!

Which team is returning nil? Also this would only change the team locally so you need to use remote events.

I’m going to give you a little guide that may help you but you have to use a remote event in order to be able to do this efficiently. So follow the instructions I’m about to ask you to do:

Step 1 - Add a RemoteEvent object to ReplicatedStorage and name it TeamChanger, it should look as so:
image

Step 2 - Change your local script in your GUI that you have showed and and replace with this:

local Teams = game:GetService("Teams")

local CivilianJoinButton  = script.Parent.Parent.Jobs.Civilian.CivilianJoin
local PoliceJoinButton  = script.Parent.Parent.Jobs.Police.PoliceJoin
local FirefighterJoinButton  = script.Parent.Parent.Jobs.Firefighter.FirefighterJoin
local DepartmentOfTransportationJoinButton  = script.Parent.Parent.Jobs.DepartmentOfTransportation.DepartmentOfTransportationJoin

local TeamChange = game:GetService("ReplicatedStorage").TeamChanger -- The event used to change the player team

local CivilianTeam = "Civilian" 
local PoliceTeam = "Police"
local FirefighterTeam = "FirefighterTeam"
local DepartmentOfTransportTeam = "DepartmentOfTransport"

CivilianJoinButton.MouseButton1Click:Connect(function()
	TeamChange:FireServer(CivilianTeam)
end)

PoliceJoinButton.MouseButton1Click:Connect(function()
	TeamChange:FireServer(PoliceTeam)
end)

FirefighterJoinButton.MouseButton1Click:Connect(function()
	TeamChange:FireServer(FirefighterTeam)
end)

DepartmentOfTransportationJoinButton.MouseButton1Click:Connect(function()
	TeamChange:FireServer(DepartmentOfTransportTeam)
end)

Step 3 - Once completed these 2 steps, finish with adding a script into ServerScriptService, here is the code needed for the script:

local TeamChange = game:GetService("ReplicatedStorage").TeamChanger
local Teams = game:GetService("Teams")

TeamChange.OnServerEvent:Connect(function(player,team)
	if team == "Civilian" then
		player.Team = Teams.CivilianTeam
	elseif team == "Police" then
		player.Team = Teams.PoliceTeam
	elseif team == "FirefighterTeam" then
		player.Team = Teams.FirefighterTeam
	elseif team == "DepartmentOfTransport" then
		player.Team = Teams.DepartmentOfTransportTeam
	end
end)

Please contact me or reply if there is any confusion with these steps or any errors you happen to receive.

Nooooooooooooooooooooooooo. I hate remote events I’m going to watch some videos on it.

1 Like

I’ve already put everything that makes the remote event work in the script. You just need to copy and paste it. If you need more advice I’m more than happy to help you. RemoteEvents just let you do things on the server-side rather than locally, vice versa, its just like sending messages through the server.

Hello, I watched some videos and I think changing it on the brick color of the team might be less code to right. Let me test it out.

Sounds good. I’ve made a copy for you so if it doesn’t work you can go over the code yourself and review how it works. The scripts I have made should be compatible with yours from what I’ve seen unless you have changed anything. Update me on how it goes, good luck!

TeamChanger.rbxl (36.5 KB)

Ello it’s works!

Code:

Local Script:
local TeamChangerRemoteEvent = game:GetService("ReplicatedStorage").TeamChanger

local CivilianJoinButton  = script.Parent.Parent.Jobs.Civilian.CivilianJoin
local PoliceJoinButton  = script.Parent.Parent.Jobs.Police.PoliceJoin
local FirefighterJoinButton  = script.Parent.Parent.Jobs.Firefighter.FirefighterJoin
local DepartmentOfTransportationJoinButton  = script.Parent.Parent.Jobs.DepartmentOfTransportation.DepartmentOfTransportationJoin

CivilianJoinButton.MouseButton1Click:Connect(function()
	TeamChangerRemoteEvent:FireServer(BrickColor.new("Fossil"))
end)

PoliceJoinButton.MouseButton1Click:Connect(function()
	TeamChangerRemoteEvent:FireServer(BrickColor.new("Dark Dark blue"))
end)

FirefighterJoinButton.MouseButton1Click:Connect(function()
	TeamChangerRemoteEvent:FireServer(BrickColor.new("Really red"))
end)

DepartmentOfTransportationJoinButton.MouseButton1Click:Connect(function()
	TeamChangerRemoteEvent:FireServer(BrickColor.new("New Yeller"))
end)

Script:
local TeamChange = game:GetService("ReplicatedStorage").TeamChanger
local Teams = game:GetService("Teams")

TeamChange.OnServerEvent:Connect(function(Player, TeamColor)
	Player.TeamColor = TeamColor
	Player.Character.Humanoid.Health = 0
end)

Very glad to hear this. You’ve now learnt how to use RemoteEvents!

Could you explain bit more on remote events? Since I don’t understand everything bout them

I was in your position with this same thing about a few months ago. So think of a RemoteEvent basically being a message being sent when an event occurs, this event can be anything from touching a part to clicking a button.

I’ll provide you with an example right now for a server sided RemoteEvent. So in a local script, in a GUI, just like you have, MouseButton1Click is an event so when you click it, an event occurs meaning something is bound to happen. So when you write this in a local script:

local Event  = game.ReplicatedStorage.Event

Button.MouseButton1Click:Connect(function()
     Event:FireServer()
end)

This will just fire the event, and if there is no script that has a .OnServerEvent for the remote event provided, then nothing will happen. All .OnServerEvent does is just run a function when you run :FireServer for an event. So in this next script (normal script - not local), I will show you how the .OnServerEvent works.

local Event = game.ReplicatedStorage.Event

Event.OnServerEvent:Connect(function(player)
       print("Hello, remote event has been fired.")
end)

Also, when using .OnServerEvent, the first parentheses will always be the player, so if you wanted to do:

local Event = game.ReplicatedStorage.Event

Event.OnServerEvent:Connect(function(player)
       print("Hello, ".. player.Name)
end)

It would print Hello with your player name. I hope that all makes sense, if it doesn’t, try to go over some more practise with it and go over this link (Handling Events) as it helped me gain more knowledge and understanding about Remote Events.