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:
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.
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.
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!
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.