Player's Team Name As A Value

Hello world! I am making a soccer script. In the game there is a StringValue that has the player’s name who ever the last player to touch the ball.

I am wanting to get the player’s team name but I don’t know how to do that when I just have the player’s name as a StringValue’s value.

local name = StringValue.Value

local player = players:FindFirstChild(name)

local player = game:GetService("Players"):FindFirstChild(stringValue.Value)
if player then
	print(player.Team)
end

Storing the player’s UserId would be a better bet. You could then use GetPlayerByUserId instead of FindFirstChild, as the latter could return an instance that is not a player in unlikely circumstances.

1 Like

A player’s team name can be known by using simply “player.Team.Name”

So the script should be like -

local touch_player = game.Players:FindFirstChild(StringValue.Value)
local touch_team = touch_player.Team

print(touch_team.Name.." team touched the ball")

I can’t get pasted this issue. This is a Print in the output. Sometime it works like I want and other times it works like this.

Problem: The player own goals because team name does not equal goal’s value. Witch is what I want, but the player is on the blue team not orange and it is printing the player is on orange team.

Player’s Team name, Player’s name, Goal’s value, Points added

OrangeTeam Cyber_Designer BlueGoalValue 0
--==============================================================================
--LOCAL SCRIPT >> STARTERGUI
--==============================================================================
--<<SERVICES>>
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
--==============================================================================
--<<VERIABLES>>
local Stats = workspace.Statss
local scoringPlayer = Stats.BallPossession
local blueGoalValue = Stats.BlueGoalValue
local orangeGoalValue = Stats.OrangeGoalValue
local player = Players.LocalPlayer
local scoreFrame = player.PlayerGui:WaitForChild("ScreenGui").ScoreFrame
local announcementLabel = scoreFrame.AnouncmentLabel
local rewardTextLabel = scoreFrame.RewardTextLabel
--==============================================================================
--<<FUNCTIONS>>
local function getTeamName(player)
	for _, team in pairs(Teams:GetTeams()) do
		if team:IsA("Team") then
			return team.Name
		end
	end
	return nil
end

local function displayGoalResult(color, text)
	announcementLabel.TextColor3 = color
	announcementLabel.Text = text
	wait(2)
	announcementLabel.Text = ""
	announcementLabel.TextColor3 = Color3.new(0.988235, 0.784314, 0.0470588)
end

local function handleGoal(goalType)
	local teamName = getTeamName(scoringPlayer.Value)
	local pointsss = "non"
	if teamName then
		if (teamName == "BlueTeam" and goalType == orangeGoalValue) or
			(teamName == "OrangeTeam" and goalType == blueGoalValue) then
			displayGoalResult(Color3.new(1, 0, 0), "OWN GOAL!")
			pointsss = "0"
		else
			if (teamName == "BlueTeam" and goalType == blueGoalValue) then
			displayGoalResult(Color3.new(0.0470588, 0.345098, 0.988235), string.upper(scoringPlayer.Value .. " SCORED"))
				pointsss = "+1"
			end
			if (teamName == "OrangeTeam" and goalType == orangeGoalValue) then
				displayGoalResult(Color3.new(0.988235, 0.407843, 0.0470588), string.upper(scoringPlayer.Value .. " SCORED"))
				pointsss = "+1"
			end
		end
		print(teamName, scoringPlayer.Value, goalType, pointsss)
	end
end
--==============================================================================
--<<CONNECTIONS>>
blueGoalValue.Changed:Connect(function()
	handleGoal(blueGoalValue)
end)

orangeGoalValue.Changed:Connect(function()
	handleGoal(orangeGoalValue)
end)
--==============================================================================