Help with unban command

  1. What do you want to achieve?
    I want to get a unban script that removes a player from the datastore.

  2. What is the issue?
    I’m not sure how to combat the player removing part of the datastore.

  3. What solutions have you tried so far?
    I’ve looked around the devforum and I havent found any useful solutions.

Below is the ban script. I’ve already tried reverting the ban command to an unban command but the player isn’t in the game because they can’t be.

local dds = game:GetService("DataStoreService")
local dataname = "blk-2"
local blkListDatastore = dds:GetDataStore(dataname)

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		local splitMsg = string.split(msg, " ")
		local cmd = splitMsg[1]
		if splitMsg[1] == "ban"then
			local target = game.Players:FindFirstChild(splitMsg[2])
			if target then
				local success, errorMessage = pcall(function()
					blkListDatastore:SetAsync(target.UserId, true)
					print("added to datastore")
					player:Kick("L")
				end)

				if not success then
					warn("Failed to add player to the datastore:", errorMessage)
				end
			end
		end
	end)
end)


game.Players.PlayerAdded:Connect(function(target)
	local success, data = pcall(function()
		return blkListDatastore:GetAsync(target.UserId)
	end)

	if success then
		if data == nil then
			-- Player is not in the datastore
			print("not in datastore")
		else
			target:Kick("in datastore")
		end
	else
		warn("Failed to retrieve data:", data)
	end
end)

Can’t you just make it so when someone is banned they get added to a table then that table gets saved then whenever you want to unban them just get the table remove their userid and save it and you can use their username so that if you get :GetUserIdFromUserName then it can get the user

I can’t add them to their table. This is because every time the server restarts/closes, the table will reset.

local bans = {}
local data = game:GetService("DataStoreService"):GetDataStore("Bans")
local defaultBanList = {1451235, 1239124} -- these arent real player userids

local function checkbans()
	for i,v in pairs(bans) do
		if game.Players:GetPlayerByUserId(v) then
			local bannedplr = game.Players:GetPlayerByUserId(v)
			bannedplr:Kick("You're banned from the game")
		end
	end
end

if data:GetAsync("BanList") then
	bans = data:GetAsync("BanList")
	checkbans()
else
	data:SetAsync("BanList", defaultBanList)
end

local function banplr(plr)
	bans[#bans+1] = plr.UserId
	data:SetAsync("BanList", bans)
end

game.Players.PlayerAdded:Connect(function()
	checkbans()
end)

made a system like this before in the past, never failed me.