Finding if the name matches from a table

Alright, so, I’ve recently tried to make some kind of script that identifies people’s team name and checks if it’s matching to at least one from a table, unfortunately, it seems to be that “==” and “=~” operators are not good for that. Any idea on how to get it working?

local teams = {"a", "b", "c"}

while wait(0.5) do
      if game.Players.LocalPlayer.Team.Name == teams then
          print("Player has team matching")
      end
end
2 Likes

Basically what happens is that when I use “==” it does nothing, while if I use “=~”, it’ll do it for any team even outside the ones in the table.

I’m pretty sure this could definitely help you:

https://developer.roblox.com/en-us/api-reference/lua-docs/table

Tried seeking for something that can help me in it but about operators, I can’t find a thing.

youll need to get all the things first on the table
you cant compare tables
you can use loops to get them and then compare it

theres also a function on tables called table.find() its a lot more easier

local teams = {"a", "b", "c"}

while wait(0.5) do
	if table.find(teams, game.Players.LocalPlayer.Team.Name) then
		print("Player has team matching")
	end
end
3 Likes

If I understood the question correctly, maybe this will help!

(This is how I would format it (my naming conventions are based on data type, sorry if it’s hard to follow), just to be more organized and efficient since you’re asking the computer to find the player, team and team name every time that runs instead of storing and referencing them)

(I removed the other method since this one’s way cleaner)

-- Services
local Players = game:GetService("Players")

-- Objects
local player = Players.LocalPlayer

-- Tables
team_names = {"a", "b", "c"}

-- Functions
local function GetLocalTeam()
    for Index, Team_Name in pairs(team_names) do -- Iterate through the array
        if player.Team.Name == Team_Name then -- Compare each value
            print("Player has team matching")
            break;
        end
    end
end

You could even use one of these depending on what you’re trying to do

local function DoesTeamMatch()
    for Index, Team_Name in pairs(team_names) do
        if player.Team.Name == Team_Name then
            return true;
        end
    end
end

if DoesTeamMatch() then
    -- Do something
end
local function PlayerTeam()
    for Index, Team_Name in pairs(team_names) do
        if player.Team.Name == Team_Name then
            return Team_Name;
        end
    end
end

print(PlayerTeam())

This might help in the future

I didn’t fully understand the question but hopefully this helps, this isn’t meant to be copy and paste, it’s meant to help you out!
Also, sorry if I made a typo or something, didn’t do too much proof reading

Alright, that works really fine! Many thanks!

In regards to this, I would do it but I’ll wait to get at this level, still thank you too!

1 Like
local teams = game:GetService("Teams")
local players = game:GetService("Players")
local player = players.LocalPlayer

for _, team in ipairs(teams:GetTeams()) do
	if player.Team == team then
		if team == teams.Red then --Player is on red team.
			--Do something with player/team.
		end
	end
end

You should be comparing team instances rather than team names. That way you can do something with the team instance once a match has been found.

It had nothing to do with teams or the player itself, it was mostly just to validate if a player is in the team appearing inside the table, but thank you.

Right, as I suggested, you could perform the same validation by checking the player’s team property against each team instance instead of the player’s team’s name property against an arbitrary table of string values.

I used the LocalPlayer as if it was a LocalScript just to show the issue in a really quick way but yes, I know the “never trust the client” thing, I use almost only Server-Sided scripts and not Locals, except if it’s for cameras or something that wouldn’t really impact on the user playing experience.