I can't display the player's team

I’ve worked on a game recently, and I want to make a GUI that displays the Player’s team. However, the script i made didn’t work. I have also looked throughout the devforum and devhub, nothing helped.

My script is stored inside of a LocalScript object:

localPlr = game.Players.LocalPlayer
TextFrame = script.Parent





TextFrame.Text = "Current team: "..localPlr.Team 

Someone please explain why it didn’t work and what i can do to fix it.

1 Like

You need to make add a remote event and run a server script inside of ServerScriptService that fires this event.

SERVER SCRIPT

game.Players.PlayerAdded:Connect(function(player)
      game.ReplicatedStorage.<EVENTNAME>:FireClient(player)
end)

LOCAL SCRIPT INSIDE OBJECT

local player = game.Players.LocalPlayer

game.ReplicatedStorage.<EVENTNAME>.OnClientEvent:Connect(function()
       script.Parent (OR OBJECT LOCATION).Text = player.Team

       GetPropertyChangedSignal("Team"):Connect(function()
              wait(0.1) -- Avoid issues
              script.Parent (OR OBJECT LOCATION).Text = player.Team
       end)
end)

Adding an event will ensure that it fires for the client only. Make sure the object location or GUI is directed to the PlayerGui. (player.PlayerGui..Text =…)

2 Likes

Try localPlr.Team.Name, since the Team property is actually an Instance.
However, you probably want to hook up a function whenever the team changes:

local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer
local Label = script.Parent

local function onTeamChanged()
    Label.Text = LocalPlayer.Team.Name
end
if LocalPlayer.Team then onTeamChanged() end
LocalPlayer:GetPropertyChangedSignal("Team"):Connect(onTeamChanged)
2 Likes

The script worked! Thank you to both of you who helped me solve this problem! :woot: