This script bans a player in the form of putting them inside a table and kicking them. When they rejoin (any player), they will go through a scan to see if they are in the table. If they are, the game kicks them because they are banned.
I cant get the unban part to work.
local bannedPlayers = {}
local function checkPlayer(player)
if bannedPlayers[player.Name] then
local reason = bannedPlayers[player.Name].reason
local banDate = bannedPlayers[player.Name].banDate
local moderator = bannedPlayers[player.Name].moderator
player:Kick("You are banned from the game. Reason: " .. reason .. ". Ban date: " .. banDate..". Moderator: "..moderator)
end
end
game.Players.PlayerAdded:Connect(function(player)
checkPlayer(player)
end)
local function banPlayer(name, reason, banDate, moderator)
bannedPlayers[name] = {reason = reason, banDate = banDate, moderator = moderator}
end
game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
local splitMsg = string.split(msg, " ")
local cmd = splitMsg[1]
if cmd == "ban" then
local target = game.Players:FindFirstChild(splitMsg[2])
if target then
print("yes")
banPlayer(target.Name, "Cheating on property!", os.date(), plr.Name)
print(bannedPlayers)
target:Kick("Banned!")
end
if cmd == "unban" then
local target = splitMsg[2]
local index = table.find(bannedPlayers, target)
if index then
print("yes~")
table.remove(bannedPlayers, index)
print(bannedPlayers)
end
end
end
end)
end)
The bannedPlayers table is a dictionary, not an array. The name is the key and the ban information is the value part. table.find() and table.remove() are only for arrays.
Therefore, this should be the following code:
if cmd == "unban" then
local target = splitMsg[2]
local banInfo = bannedPlayers[target]
if banInfo then
print("yes~")
bannedPlayers[target] = nil -- Not the index since the index is actually considered the ban information
print(bannedPlayers)
end
end
From the quote above (which is the (simplified) code from your original post), it seems like you are checking if cmd is defined as "ban", then checking if cmd is defined as "unban" right after confirming cmd is defined as "ban". This doesn’t make any sense if you need to check if cmd is defined as "unban", because you already confirmed that it is defined as "ban".
(Sorry if my explanation was messy, you can just read what you have to do below if you want to save time.)
Try taking the unban if statement out of the ban if statement (as shown below), so it should run if cmd is not defined as "ban" and also that it is defined as "unban".
if cmd == "ban" then
-- do banning code here
elseif cmd == "unban" then
-- do unbanning code here
end