Unable to fetch player team

While scrolling through the forums, I noticed someone trying to make a script that identified a team with the last player “alive” in the server. I thought it would be some good practice to try to make this, so I did. The demo works as follows: Each team has a bool value assigned to it indicating the team’s “alive” status. There is a part located in the workspace for me to walk up to and touch. Upon doing so, all bool values will be set to false aside from that of the team belonging to the player that touched the part, indicating that the player that touched the part is the last player “alive.” A gui/frame/text label are then created indicating the team that won.

However, when I was done with the demo, I noticed I had some bizarre issue where the team I was fetching from the player that triggered a touched event was not the same as the player’s actual team. Below is some more info on what I have going on.

My current set up is as follows:
There are 3 teams, Red, Green, and Blue.
In each team, there is a bool value (called “Value” by default) set to true
There is a Part in workspace that has a script (not local) that identifies the player that touches the part, sets the bool values in all the teams that are not the player’s team to false, then creates an output indicating the winner. In this case, it creates a GUI in PlayerGUI.

The script located in the part is as follows:

script.Parent.Touched:Connect(function(part)
	local success, player = pcall(function()
		return game.Players:GetPlayerFromCharacter(part.Parent)
	end)
	local Teams = game.Teams:GetTeams()
	if success then
		print(player.Team)
		for _, i in ipairs(Teams) do
			if tostring(player.Team) ~= tostring(i) then
				i.Value.Value = false
			else 
				UI = Instance.new("ScreenGui")
				UI.Parent = player.PlayerGui
				Frame = Instance.new("Frame")
				Frame.Parent = UI
				Frame.Size = UDim2.new(0, 300, 0, 200)
				Label = Instance.new("TextLabel")
				Label.Parent = Frame
				Label.Size = UDim2.new(0, 300, 0, 200)
				Label.Text = "Team " .. tostring(i) .. " wins."
			end
		end
	end
end)

I read the documentation for a few of the classes I was using, but I didn’t find anything that solved my problem. I also read a few of the related topics generated while creating this post since this is my first post. Similarly, those didn’t yield any results.

I’m always willing to provide more info if need be. :slight_smile:

(Edit): Some more information on the bug, on line 7, I print the player’s team to the console, which I fetched through getting the player’s Player from their character model, then getting that Player's team. However, every time I do this, regardless of what that Player's team is, the red team is output to the console.

(Solution): All the teams are of the same BrickColor. Because Roblox uses this to differentiate between teams, when Roblox tried to identify a specific team of mine when they all had the same BrickColor, it would think all of my teams were the same team and default to team red.

1 Like

the player.Team is just an array so if you make it a string it will just, "" or nil. i recommend you changing it to player.TeamColor and i.TeamColor

Okay this is a little offtopic but why did you wrap

game.Players:GetPlayerFromCharacter(part.Parent)

in pcall?
I’m not saying it’s wrong, just curious.

1 Like

Are there any errors in the output? If so, could you please state them? It would really help me help you :slight_smile:

:bowing_woman:t2:Kon’niciwa!
Player.Team should be Player.Team.Name if you are trying to get the team name/string.

I believe this is the answer you are looking for.

when everything first loads in, the baseplate would fire the part.Touched event

I was able to work out all of the errors, there’s just a mysterious bug that’s causing unusual behavior. On line 7, you can see I print the plater’s team. However, every time the script reaches that print statement, team red is the output.

player.Team will return the player’s team object, and the table Teams is full of team objects, casting either of them to strings will return the team name. To be safe, I tested your suggestion and the bug is still there. I still appreciate the help though :slight_smile:

However, I’ll still be sure to use Team.Name versus tostring(team)

in that case woulden’t if be more efficeint if you just used if paret.Parent then?

script.Parent.Touched:Connect(function(part)
	if part.Parent ~= nil then
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		local Teams = game.Teams:GetTeams()
		print(player.Team)
		for _, i in ipairs(Teams) do
			if tostring(player.Team) ~= tostring(i) then
				i.Value.Value = false
			else 
				UI = Instance.new("ScreenGui")
				UI.Parent = player.PlayerGui
				Frame = Instance.new("Frame")
				Frame.Parent = UI
				Frame.Size = UDim2.new(0, 300, 0, 200)
				Label = Instance.new("TextLabel")
				Label.Parent = Frame
				Label.Size = UDim2.new(0, 300, 0, 200)
				Label.Text = "Team " .. tostring(i) .. " wins."
			end
		end
	end
end)

You can’t use part.Parent because your accessory may touch the part then will return nil finding the user because the ClassName is a accessory. Instesd use if statement then add the part.Parent:FindFirstChild("Humanoid") if it returns true then its a character if not then no.

1 Like

actually, I just realised, you don’t even need to do “if”, Basepart’s parent is workspace, and
game.Players:GetPlayerFromCharacter(game.Workspace.Basepart) returns nil, so all you have to do is check, what the functions returned is nil or not