PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Whitelisting Players

Whitelisting Players

Jun 23 2018, 2:02 AM PST

Problem

You want to remove the right arm of everyone except for the people you specify in a table.

Solution

Use the player names as keys in a table, each with the value of true, then index the current player name while iterating over all of the game’s players.

local function makeLookupTable(table)  -- Utility function to create a lookup table
	for i = 1,#table do
		table[table[i]] = true
	end
	return table
end

local safePlayers = makeLookupTable({"BobBob", "Camoy", "Mikey"})

for _, player in ipairs(game.Players:GetPlayers()) do
	if not safePlayers[player.Name] then
		local ra = player.Character:FindFirstChild("Right Arm")
		if ra then
			ra:Destroy()
		end
	end
end

Discussion

We first need a table where the keys are the player names (in Lua you use a key to index and the corresponding value will be returned). The values of these keys can be anything except nil or false since those would make a conditional not pass. The makeLookupTable() function simplifies this task by taking a normal table of names as values, adding those names as keys, and setting each to a value of true.

The lookup table is filled by the command on line 8 — just list the names of all “safe” players as strings inside the table you pass to the makeLookupTable() function.

Next, we loop over all of the game’s players and check if not safePlayers[player.Name] then in which !player is a reference to one of the players in the game (different for each iteration of the loop since it runs once for each player in the game). The Instance/Name|Name property (player.Name) returns the name of the object; in this case, all player objects are named by the user name of the person they belong to. If the player’s name is not found in the safePlayers table, we find that player’s right arm and, if it exists, we run the Instance/Destroy|Destroy() command to remove the arm.

Tags:
  • players
  • event
  • whitelist
  • script