You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
Make it display a player’s team on a GUI on my custom playerlist.
What is the issue? Include screenshots / videos if possible!
I have no idea what types of text I need to type into a script to display a player’s team.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried looking for different YouTube videos and different DevForums but nothing helped.
I’m not asking for someone to type a full script out. I would just like for someone to point me in the right direction to get a working script!
Thank you.
local textLabel = script.Parent -- change to your right path
local teams = game:GetService("Teams")
local players = game:GetService("Players")
local player = players.LocalPlayer
--initial apply
textLabel.Text = player.Team.Name
--when the player changes team , apply the change
player:GetPropertyChangedSignal("Team"):Connect(function()
textLabel.Text = player.Team.Name
end)
Thank you for your reply!
I’ve only got it to partially work. The text label’s text is replaced with the first team that the players spawns in as, but when the team is changed, the text label doesn’t change (I feel like this could be fixed by looping the script though). Something that I’ve also noticed is that the script is crashing the game in studio and when the game is published, it won’t start the game at all.
This has already been applied in the last part of @Valkyrop 's script.
player:GetPropertyChangedSignal("Team"):Connect(function()
textLabel.Text = player.Team.Name
end)
-- unless teams are being changed client-sided and this code is in a server script, there shouldn't be any problem
I don’t think this is related to the script that was given. You should check through other scripts that you believe could be causing such behaviour.
This seems too much for a simple script. A better way to do this is to instead use tables to keep the script neat and organized.
-- code has been edited
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local player = Players.LocalPlayer
local TextLabel = script.Parent
-- this is all just an example
local listOfTeamNames = { -- you can list the Team Instances, Team Names, or Team Colors here
"Team1", -- change these strings to the actual name of your teams
"Team2"
-- etc.
}
local listOfStrings = {}
listOfStrings["Team1"] = "TEXT TO DISPLAY FOR TEAM1" -- display text for this team
listOfStrings["Team2"] = "TEXT TO DISPLAY FOR TEAM2"
-- if you have more teams you'd like to add, just rewrite the line of code above and make sure to set the first string as the team name
for index, teamNames in pairs(listOfTeamNames) do
if player.TeamColor == teamNames then
TextLabel.Text = listOfStrings[player.Team.Name]
end
end
player:GetPropertyChangedSignal("TeamColor"):Connect(function()
for index, teamNames in pairs(listOfTeamNames) do
if player.TeamColor == teamNames then
TextLabel.Text = listOfStrings[player.Team.Name]
end
end
end)
Also, if the TextLabel is under a GuiObject, it’s better to have the text changed via LocalScript to avoid any confusion between server and client accessibility.
Yes. Make sure it’s a local script. I’m going to rewrite the script above so it is a bit more easier for you to understand without any confusion. Should take about a minute (I’m going to edit the post above).
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local player = Players.LocalPlayer
local TextLabel = script.Parent
local listOfTeamNames = { -- you can list the Team Instances, Team Names, or Team Colors here
"Farmer",
"Mechanic",
"Unemployed"
}
local listOfStrings = {}
listOfStrings["Farmer"] = "Occupation: Farmer" -- If you intend to change the display text, change it right here
listOfStrings["Mechanic"] = "Occupation: Mechanic" -- same goes for the rest of the teams
listOfStrings["Unemployed"] = "Occupation: Unemployed"
--[[
if you have more teams you'd like to add, first make sure to add an extra string
in the 'listOfTeamNames' table first, then rewrite the line of code above and
change the first string to the name of your new team
Example of adding new teams:
local listOfTeamNames = {
"Farmer",
"Mechanic",
"Unemployed", -- add a comma or semicolon after the previous string to add a new string in the table
"Engineer" -- new team name
}
local listOfStrings = {}
listOfStrings["Farmer"] = "Occupation: Farmer"
listOfStrings["Mechanic"] = "Occupation: Mechanic"
listOfStrings["Unemployed"] = "Occupation: Unemployed"
listOfStrings["Engineer"] = "Occupation: Engineer" -- new team
]]
for index, teamNames in pairs(listOfTeamNames) do
if player.Team.Name == teamNames then
TextLabel.Text = listOfStrings[player.Team.Name]
end
end
player:GetPropertyChangedSignal("TeamColor"):Connect(function()
for index, teamNames in pairs(listOfTeamNames) do
if player.Team.Name == teamNames then
TextLabel.Text = listOfStrings[player.Team.Name]
end
end
end)
-- All teams (for now): Farmer, Mechanic, Unemployed
The reason why it didn’t work was because you did not change the strings to the name of your teams.
Edit:
I’ve found a little mistake I made in the code while testing, I have just fixed the code and tested it in studio. I’ve edited the code in this post. It should be working fine now.