Scripting issue with if statement

I’m making a script that will send a message in chat when a player joins a team and I want it to ignore one of the teams so I used an if statement but it still gets triggered and I’m not sure how.

This is the code

local TeamsService = game:GetService("Teams")
local remote = Instance.new("RemoteEvent", game.ReplicatedStorage)
remote.Name = "ServerMessage"

for _, team in pairs(TeamsService:GetTeams()) do
	team.PlayerAdded:connect(function(player)
		if player.team ~= "Menu" then
			remote:FireAllClients("[System] " ..player.Name.. " has joined " ..team.Name, Color3.fromRGB(15, 179, 255), Enum.Font.Arial, Enum.FontSize.Size24)
			warn("Remote fired")
		end
	end)
end


b4601bb8eccf60b6ecb21f29790152d6

The issue is even though you are checking if someone is on a particular team, the FireAllClients() function will still fire the remote event to everyone in the game, meaning everyone will still see it happening. If I were you, I would loop through teams that you want to see the message, and then use FireClient() instead.

Kf637, the problem here is your conditional statement is checking if player.team ~= "Menu" while it should be checking if player.team.Name ~= "Menu". Hope this helps!

2 Likes