Exclude an instance from a table

So I’m trying to make a system on how you can put a player name in a table and that player is then excluded to later functions that affect all the player but how would i actually exclude that player

target ~= game.Players:FindFirstChild(table.find(_G.excludedplayers)) then

this is what i have rn but it doesn’t work

Maybe try to use player names instead of the actual player instance.

still doesn’t work i think the main problem is how i’m getting the player name from the table

To be honest, I never used global variables so I don’t really know how it works, but usually to check a value in a table I use table[name], however I am not sure if that would work in this case. What exactly is stored in the table?

idk what you really mean but i’m guessing this is what u want?

if _G.excludedplayers[target] then
   -- the target is in the table?
end

_G, is just a normal table, its just shared
there’s nothing special about it

1 Like

so i pretty much have just normal usernames in the table but like i want them to be excluded from like a for i, players(game.Players:GetPlayer() loop

ok so
you have a tbl with players you want to exclude

local player_table = {...}

you want to exclude every player in that table:

local player_table = {...}
for _, plr in game.Players:GetPlayers() do
    if player_table[plr] then
        continue
    end

    print(plr, "is not excluded")
end

if you find it helpful, i’d appreciate it if
you can solution it! :smiley:

1 Like

no but it’s like only the player names that are in the table not the players itself so this wouldn’t work

For your specific problem:

local PlayerService		= game:GetService("Players")
local exludedPlayers	= {PlayerService.LocalPlayer}

local function someFunction()
	for i, player in pairs(PlayerService:GetPlayers()) do
		if table.find(exludedPlayers, player) then continue end

		print(player.Name)
		--Do your function here
	end
end

A more general version:

local PlayerService	= game:GetService("Players")

local function getIncludedPlayers(excluded)
	local array = PlayerService:GetPlayers()
	excluded = if excluded then excluded else {}

	for i, player in pairs(excluded) do
		table.remove(array, table.find(array, player))
	end

	return if array then array else nil
end

print(getIncludedPlayers())

getIncludedPlayers() will return an array of all players in the game, that aren’t in the exclusion paramters array.

If you want to use the playernames in the excluded table, you can do something like this:

local PlayerService	= game:GetService("Players")

local function getIncludedPlayers(excluded)
	local nameArray = {}
	local array 	= PlayerService:GetPlayers()
	excluded = if excluded then excluded else {}

	for i, player in ipairs(array) do
		table.insert(nameArray, player.Name)
	end

	for i, playerName in pairs(excluded) do
		local index = table.find(nameArray, playerName)
		table.remove(nameArray, index)
		table.remove(array, index)
	end

	return if array then array else nil
end

if its the players names you can do smth like this:

local players_names = {...} -- excluded names
for _, plr in game.Players:GetPlayers() do
    if player_table[plr.Name] then
        continue
    end

    print(plr.Name, "is not excluded")
end

i kind of want more like a result where’d i’d be able to do like

for i, v in pairs(game.Players:GetPlayers()) do
if v ~= table.find(_G.excludedplayers) then
print(v)
end
end

would want the output to be every player except for the players in the table since i’ve only just decided to add a way to exclude players i’d have to change everything if i used one of the way above

for i, player in pairs(game.Players:GetPlayers()) do
	if not table.find(_G.excludedplayers, player.Name) then continue end
	print(player)
end

This should work in the way you expect it to - However I don’t think it is the best way to do this.

The script isn’t to complicated and i’m not really to advanced in programming so i’d like to keep it a way i can understand what is going on:)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.