Hey there so I am trying to make a list of players or a table of players that are banned from a server.
Script:
local bancommand = "!ban "
local banlist = {}
local function banFunc(name)
for _, player in pairs(game.Players:GetPlayers()) do
local targetedPlr = string.find(player.Name:lower(),name)
if targetedPlr then
table.insert(banlist, player.UserId)
player:Kick("You have been banned!")
end
end
end
game.Players.PlayerAdded:Connect(function(player)
if table.find(banlist,player.UserId) then
player:Kick(“You can’t join back because you aren banned.”)
end
end)
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
if string.sub(message,1,string.len(bancommand)):lower() == bancommand:lower() then
local UserName = string.sub(message,string.len(bancommand) + 1):lower()
banFunc(UserName)
end
end)
end)
Could I (we) get more context on what you are troubled with? Do you want it saved temporarily, to the particular server alone, or permanently. Furthermore, is the code snip-it you provided working, or does not work and you need help fixing it?
I had mentioned using DataStoreService, however indirectly. And it’s actually way less work than trying to save a whole table! (Instead of editing the entire table, you can just edit their datastore.)
That’s a lot of requesting. When the server starts, you can load the entire table into memory and update it as you go instead of querying each player in the DataStore.
please help guys I don’t know also I tried using bool value but the value would save its just that if it was true it wouldn’t still kick player
script:
local dataStoreService = game:GetService(“DataStoreService”)
local dataStore = dataStoreService:GetDataStore(“dataStore”)
local bancommand = "!ban "
local function banFunc(name)
for _, player in pairs(game.Players:GetPlayers()) do
local targetedPlr = string.find(player.Name:lower(),name)
if targetedPlr then
player:FindFirstChild("bannedVal").Value = true
player:Kick("You have been banned!")
end
end
end
game.Players.PlayerAdded:Connect(function(player)
if player:FindFirstChild(“bannedVal”) == true then
print(“Banning again”)
player:Kick(“You can’t join back because you are banned.”)
print(“Banned”)
end
end)
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
if string.sub(message,1,string.len(bancommand)):lower() == bancommand:lower() then
local UserName = string.sub(message,string.len(bancommand) + 1):lower()
banFunc(UserName)
bannedVal.Value = true
end
end)
local playerId = "Player_"..player.UserId
local data
local suc,err = pcall(function()
data = dataStore:GetAsync(playerId)
end)
if suc then
bannedVal.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerId = “Player_”…player.UserId
local data = player:FindFirstChild("bannedVal").Value
local suc, err = pcall(function()
dataStore:SetAsync(playerId,data)
end)
if suc then
else
warn(err)
end
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("dataStore")
local bancommand = "!ban "
function loadData()
local data
local success, err = pcall(function()
data = dataStore:GetAsync("bannedPlayers")
end)
if success then
if data == nil then
data = {}
end
return data
else
print(err)
end
end
function saveData(data)
local loadedData = loadData()
loadedData[#loadedData + 1] = data
local success, err = pcall(function()
dataStore:SetAsync("bannedPlayers", loadedData)
end)
if not success then
print(err)
return false
else
return true
end
end
local function banFunc(name)
local targetedPlr = game.Players:FindFirstChild(name)
if targetedPlr then
if saveData(targetedPlr.UserId) then
targetedPlr:Kick("You have been banned!")
else
-- Put what you want to happen if the data could not be saved
end
end
end
game.Players.PlayerAdded:Connect(function(player)
local loaded = loadData()
if table.find(loaded, player.UserId) then
print("Banning again")
player:Kick("You can’t join back because you are banned.")
print("Banned")
else
player.Chatted:Connect(function(message)
if string.sub(message, 1, #bancommand):lower() == bancommand:lower() then
local UserName = string.sub(message, #bancommand + 1, #message)
banFunc(UserName)
end
end)
end
end)