Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Ban System

I am having trouble making a ban System. What I mean is that I will be able to use ROBLOX studio and type in the players name and they are banned from the server.

5 answers

✓ Accepted answer

Here's a way that uses indexes instead of table values. It makes searching for the name a lot faster. Don't worry about the =true; part and just copy the style.

local BannedList = {
	["MrNicNac"] = true;
	["Lycan"] = true;
	["Shedletsky"] =true;
}

function NewPlayer(pl)
	if BannedList[pl.Name] then
		wait()
		pl:Kick();
	end
end

Game.Players.PlayerAdded:connect(NewPlayer)
table.foreach(Game.Players:GetPlayers(), function(i,v) NewPlayer(v) end) -- Makes it function in solo mode

Something that neither of these answers capitalized upon is the use of the new and improved data stores. These data stores will provide you with a way to preserve your data across all servers, and it will update in real-time, so if they're banned from one server, they're immediately banned from all servers in that game.

Let's analyze the API dump for data stores:

Class DataStoreService : Instance
	Function Instance DataStoreService:GetDataStore(string name, string scope = global)
	Function Instance DataStoreService:GetGlobalDataStore()

Class GlobalDataStore : Instance
	Function Connection GlobalDataStore:OnUpdate(string key, Function callback)
	YieldFunction Variant GlobalDataStore:GetAsync(string key)
	YieldFunction Variant GlobalDataStore:IncrementAsync(string key, int delta = 1)
	YieldFunction void GlobalDataStore:SetAsync(string key, Variant value)
	YieldFunction Tuple GlobalDataStore:UpdateAsync(string key, Function transformFunction)

This API dump tells us what members the related objects have, and using these we can guess at how to use them. However, in some cases we won't be able to guess, or it would be better not to. In those cases, we just need to look for a guide on the wiki. What do you know? There is one! Now we know for sure how to use it.

Now that we have all the required components to create the solution to our problem, we can go forth and actually code it. Let's make a list of the things we want our solution to take care of:

  • Making a minimum list of banned players
  • Banning people on the fly for all servers
  • Unbanning people on the fly for all servers
local DataStoreService = Game:GetService("DataStoreService")
local persistentBannedList = DataStoreService:GetDataStore("PersistentBannedList")

-- this syntax is an alternate way to write
-- data within tables, it still works though
local minimumBannedList = {
	["PlayerX"] = true;
	["PlayerY"] = true;
	["PlayerZ"] = true;
}

function isPlayerBanned(player)
	return minimumBannedList[player.Name]
		or persistentBannedList:GetAsync(player.Name)
end
function setBanStatus(player, status)
	minimumBannedList[player.Name] = status
	persistentBannedList:SetAsync(player.Name, status)
end
script.SetBanStatus.OnInvoke = setBanStatus

function onPlayerAdded(player)
	if isPlayerBanned(player) then
		player:Kick()
	end
end
Game:GetService("Players").PlayerAdded:connect(onPlayerAdded)

Note that I have added an extra feature which requires a BindableFunction to be used properly. The BindableFunction object allows you to, from one script, call functions defined in another script. The API dump for this object is below too:

Class BindableFunction : Instance
	YieldFunction Tuple BindableFunction:Invoke(Tuple arguments)
	Callback Tuple BindableFunction.OnInvoke(Tuple arguments)

Note that there is a caveat associated with this ban system. Users can change usernames. This is not an often occurence, but when it does happen, your ban system will not know, and it will allow them into your game. This is possible to fix, but if it is really necessary, you should be able to perform that fix.

Sources:

It would have been helpful if you provided a little more detail, but I think I understand. You want to type a player's name into a script and have them banned from the server. Try out this script, it's very simple and hopefully you can understand how it works:

BannedPlayers = {"Player1", "Noob" } 


game.Players.PlayerAdded:connect(function(Player)
	for i,v in pairs(BannedPlayers) do 
		if Player.Name == v then
			Player:Destroy()
		end
	end
end)

Alternatively, if you wanted to kick someone instead of simply destroying their character, you can also try this:

game.Players.PlayerAdded:connect(function(Player)
    if Player.Name == "Noob" then
        Player:Kick()
    end
end)

*Side note, neither of these scripts will work in Solo mode, you must have a server opened.

game.Players.PlayerAdded:connect(function(plr)
	if plr.Name == "PlayerName" then -- Replace "PlayerName" with Name
		plr:kick()
	end
end)

Probably easiest way to do it

This discussion is preserved as it was. New answers, comments, and votes are closed.