I remade the kick script on https://developer.roblox.com/en-us/api-reference/function/Player/Kick into a ban script. I used DataStores to store the banned player. When I use a DataStore Editor, I can add the person. But when I use the command, it doesn't work. Here is my script:
--ban command
local Players = game:GetService("Players")
local ban = "/ban"
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("myDataStore")
local function onOwnerChatted(player, message)
if message:sub(1, ban:len()):lower() == ban:lower() then
local name = message:sub(ban:len() + 1)
local playerToban = Players:FindFirstChild(name)
if playerToban then
local success, eror = pcall(function()
DataStore:SetAsync(player.UserId.."-banned",true)
end)
if success then
playerToban:Kick("You have been banned by the owner.")
print("Banned")
else
warn(eror)
game.ReplicatedStorage.Notifications:FireClient(player,name.." is not a player!")
end
end
end
end
local function onPlayerAdded(player)
if player.UserId == game.CreatorId and game.CreatorType == Enum.CreatorType.User then
player.Chatted:Connect(function (...)
onOwnerChatted(player, ...)
end)
end
end
for _, player in pairs(Players:GetPlayers()) do
onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)
--checking for banned person on different script when they join (this works)
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local banned
banned = DataStore:GetAsync(player.UserId.."-banned")
if banned == true then
player:Kick("You are banned from this game by the owner!")
print(player.Name.." is banned.")
else
print(player.Name.." is not banned")
end
end)

3 comments
If it's not working, then it should be printing the "eror" message? If it does print it, do you mind posting it alongside your code?
It does not print the error message.
I rewrote the code and now it works