Very simple and fast.
Source Code:
-- Services
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
-- DataStore
local BanDataStore = DataStoreService:GetDataStore("BanDataStore")
-- Admin UserIDs
local adminUserIds = {
4340466686
}
-- Function to check if a player is an admin
local function isAdmin(player)
for _, userId in ipairs(adminUserIds) do
if player.UserId == userId then
return true
end
end
return false
end
-- Function to ban a player by username
local function banPlayerByUsername(admin, usernameToBan)
if isAdmin(admin) then
local success, userIdToBan = pcall(function()
return Players:GetUserIdFromNameAsync(usernameToBan)
end)
if success and userIdToBan then
-- Set the ban in the DataStore using the user ID
local banSuccess, banErr = pcall(function()
BanDataStore:SetAsync(tostring(userIdToBan), true)
end)
if banSuccess then
print(usernameToBan .. " (User ID " .. userIdToBan .. ") has been banned by " .. admin.Name)
-- Optionally kick the player if they are currently in the game
for _, player in pairs(Players:GetPlayers()) do
if player.Name == usernameToBan then
player:Kick("You have been banned from this game.")
break
end
end
else
warn("Error setting ban data: " .. banErr)
end
else
admin:SendNotification("Notification", "User not found or error retrieving User ID.", 5)
end
else
admin:SendNotification("Notification", "You can't use this", 5)
end
end
-- Command listener for the /ban command
Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
local args = message:split(" ") -- Split the message into parts
if args[1] == "/ban" and args[2] then
banPlayerByUsername(player, args[2])
end
end)
end)
-- Check for bans on player join
Players.PlayerAdded:Connect(function(player)
local success, isBanned = pcall(function()
return BanDataStore:GetAsync(tostring(player.UserId))
end)
if success and isBanned then
player:Kick("You are banned from this game.")
elseif not success then
warn("Error retrieving ban data for player: " .. player.Name)
end
end)