Script not removing player name from table

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)
1 Like

Does “yes~” print in the output?

No, it doesnt. No output is shown when I try it out.

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

Can you print target and print bannedPlayers? Loop through the table and print if each value is equal to target, also print them both.

print(target, bannedPlayers)

for i, v in pairs(bannedPlayers) do
     print(target, v, v==target)
end

image
I get this

That’s my bad. I forgot to finish the code.
The post was changed immediately.

:smile:

It doesnt print the “yes~” in the script even though the parameters are correct.

Sorry but where would you like me to put this?

Are you sure that you’re typing their full username?

Yes.
image

Btw the join was me making sure the user is banned. It didnt unban them.

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
1 Like

Haha my mistake… Thanks for spotting that for me! It works perfectly fine now.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.