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
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
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())
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
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.