Team Checker does not work

Basically whenever a Player respawns and they are on one of the following teams they are supposed to get a sound object added to their PrimaryPart.
Even though I am certain that I am on the correct team, the print message does not appear in the console (“Team is correct!”), which indicates that the script is not working.

I have no idea what the cause for this could be. Keep in mind this is a regular script.

game.Players.PlayerAdded:Connect(function(Player)
	print("Player Added")
	Player.CharacterAdded:Connect(function(Character)
		print("Character Added")
		local team = Player.Team
		if team ~= nil then
			if team.Name == "Autobahnpolizei" or team.Name == "Feuerwehr" or team.Name == "Polizei" or team.Name == "Rettungsdienst" or team.Name == "SEK" or team.Name == "THW" or team.Name == "Zoll" then
				print("Team is correct")
				Player.PlayerGui.FunkGui.Enabled = true
				
				local sound = Instance.new("Sound")
				sound.Volume = 0.1
				sound.RollOffMaxDistance = 10
				sound.RollOffMinDistance = 1
				sound.Parent = Character
			end
		end
	end)
end)
game.Players.PlayerAdded:Connect(function(Player)
	print("Player Added: " .. Player.Name)
	Player.CharacterAdded:Connect(function(Character)
		print("Character Added: " .. Player.Name)
		local team = Player.Team
		if team ~= nil then
			local teamName = team.Name
			print("Team is correct: " .. teamName)
			Player.PlayerGui.FunkGui.Enabled = true
			
			local sound = Instance.new("Sound")
			sound.Volume = 0.1
			sound.RollOffMaxDistance = 10
			sound.RollOffMinDistance = 1
			sound.Parent = Character
		end
	end)
end)

Well as I have explained the if team.Name == "Autobahnpolizei" or team.Name == "Feuerwehr" or team.Name == "Polizei" or team.Name == "Rettungsdienst" or team.Name == "SEK" or team.Name == "THW" or team.Name == "Zoll" then line is essential to determine, which team gets the sound added into their character.

Then add more debugs to find out whats nil or something

Try this:


Script

local Teams = { --Make a table of all the teams you can be on (Replace with your team names)
	"Team1",
	"Team2",
	"Team3",
}

game.Players.PlayerAdded:Connect(function(Player)
	print("Player Added")
	Player.CharacterAdded:Connect(function(Character)
		print("Character Added")
		local team = Player.Team
		if team ~= nil then
			if Teams[team.name] then--Check if player is on any of the teams in the list
				print("Team is correct")
				Player.PlayerGui.FunkGui.Enabled = true

				local sound = Instance.new("Sound")
				sound.Volume = 0.1
				sound.RollOffMaxDistance = 10
				sound.RollOffMinDistance = 1
				sound.Parent = Character
			end
		end
	end)
end)

Make sure that the player gets assigned a team before this runs

Sadly this still does not fix my issue and I get no error code.

My recommendation would be to take a different approach:

for i, v in pairs(game:GetService('Teams'):GetChildren()) do
	v.PlayerAdded:Connect(function(Player)
		local team = v
		print(team)
		if team.Name == "Autobahnpolizei" or team.Name == "Feuerwehr" or team.Name == "Polizei" or team.Name == "Rettungsdienst" or team.Name == "SEK" or team.Name == "THW" or team.Name == "Zoll" then
			print("Team is correct")

			Player.PlayerGui.FunkGui.Enabled = true

			local sound = Instance.new("Sound")
			sound.Volume = 0.1
			sound.RollOffMaxDistance = 10
			sound.RollOffMinDistance = 1
			sound.Parent = Player.Character
		end
	end)
end

This way, you can also get the player when they change teams as well :slight_smile:

– Also one more thing, it’s best to edit the playerGui stuff in local scripts, I would recommend sending a remote event to the client for that

This would have worked if you assigned each team name to true. Since it’s just a table, table.find will have to be used.

Try it. And are you sure that you don’t have typos in the team names and that they have the same case?

local teams = {"Autobahnpolizei", "Feuerwehr", "Polizei", "Rettungsdienst", "SEK", "THW", "Zoll"}

game.Players.PlayerAdded:Connect(function(Player)
	print("Player Added")
	Player.CharacterAdded:Connect(function(Character)
		print("Character Added")
		if table.find(teams, Player.Team.Name) then
			print("Team is correct")
			Player.PlayerGui.FunkGui.Enabled = true

			local sound = Instance.new("Sound")
			sound.Volume = 0.1
			sound.RollOffMaxDistance = 10
			sound.RollOffMinDistance = 1
			sound.Parent = Character
		end
	end)
end)

Table["Hello"] Finds anything that is “Hello” in that table. if its not there it will return nil (nil equals False).

the if statement checks for anything True, so if it exists (does not equal nil) it returns True.


Same with instances. if a part exists then continue


I think something else was wrong in the code like not setting the team before the player joins/respawns (But I don’t think that’s it)