Script That Changes When You Change Teams | Scripting Help

Hello. I am trying to make a script that automatically gives you a gui if you are on a certain team. It removes it if you leave that team. I am having trouble and I would appreciate it if someone could help out.

My current script:

local Players = game:GetService(“Players”)
local Teams = game:GetService(“Teams”)
local Allowed_Team = Teams:FindFirstChild(“Host”)

local function TeamCheck(Player)
if Player.Team.Team == Allowed_Team then
local GUI = game.ServerStorage.HostGUI:Clone()
local PlayerGui = Player:WaitForChild(“PlayerGui”)
if PlayerGui then
GUI.Parent = PlayerGui
end
else
local PlayerGui = Player:WaitForChild(“PlayerGui”)
local NGUI = PlayerGui.HostGUI
NGUI:Destroy()
end
end

while true do
TeamCheck()
wait(0.7)
end

This should work:

local TeamsService = game:GetService("Teams")
local ServerStorage = game:GetService("ServerStorage")
local GUI = ServerStorage:FindFirstChild("HostGUI")
local Team = TeamsService:FindFirstChild("Host")

Team.PlayerAdded:Connect(function(plr)
	if plr then
		GUI:Clone().Parent = plr.PlayerGui
	end
end)

Team.PlayerRemoved:Connect(function(plr)
	if plr then
		plr.PlayerGui:FindFirstChild("RefereePanel"):Destroy()
	end
end)

Learn more on Team.PlayerAdded and Team.PlayerRemoved

Hello,

I do not think that the prior respond answered your question. So let me help you out.

You need 1 event and a function
Event : PlayerAdded
Function : that detects the Team Changed

-- Global variables
Teams = game:GetService(“Teams”)
Allowed_Team = Teams:FindFirstChild(“Host”)

game.Players.PlayerAdded:Connect(function(player) -- player joins

  -- player changes their team
  player:GetPropertyChangedSignal("Team"):Connect(function()

   -- they are now on the Allowed_Team
   if (player.Team == Allowed_Team) then
      -- giving them the UI
      local GUI = game.ServerStorage.HostGUI:Clone()
      GUI.Parent = player.PlayerGui

   else -- they are not on the allowed team
     if (player.PlayerGui:FindFirstChild("HostGUI") then -- checking if they have the UI
       player.PlayerGui.HostGUI:remove() -- remove the UI if they have it
     end
   end
  end)
end)

I hope this helps you out.

  • DosenSuppe2_0
1 Like