Help with Ban table

local Bans = {"Username", 0000000} -- add a username (string) or a userid (number)

function checkBan(player)
   for _,banned in pairs(Bans) do
      if type(banned) == "string" and player.Name == banned then
         return true
      elseif type(banned) == "number" and player.UserId == banned then
         return true
      end
   end
   return false
end

game:GetService("Players").PlayerAdded:Connect(function(player)
   if checkBan(player) == true then
      player:Kick("Banned")
   end
end)

Thanks, I will try this out, and let you know :grinning: :+1:

Using a username as a directory is a very bad idea

See my previous post, post#7 I think? I’m making a ban system, not a black list

U can just change your username and now u don’t get kicked anymore

Yeah I know, thats why I said its a bad idea.

I just showed it as an example, he can just add a userId.

As I said, your post doesnt help, because I’m not making a black list, I’m making a ban system.
I don’t want to exclude certain players, i want to make a system that will ban any player that does a certain action and then saving that ban data.

How would I save the PlayerDataTable?

I would recommend looking into DataStore2 or ProfileService you could also make your own.

So you can save tables with DataStore2 or ProfileService?

Could you tell me how to save tables? because that was my original question.

Yeah you need to use some sort of datastore to save the table.

That was my original question, how would I do that?

I already mentioned looking into Datastore2 or ProfileService. There are many post and videos about it. They have all the information and documents required to get started.

You need to put it in a pcall function.
The script probably timed out

I think your script should look like this…


local SaveData = game:GetService("DataStoreService"):GetDataStore("SaveBannedData")

function saveData(data, key)
	local info = data
	local success, err = pcall(function()
		SaveData:SetAsync(key, info)
	end)
	if success then
		print("Success!")
		return "Success"
	end
	if err then
		return false
	end
end
function readData(key)
	local success, currentExperience = pcall(function()
		return SaveData:GetAsync(key)
	end)
	if success then
		return currentExperience
	else
		return false
	end
end
function removeData(key)
	local success, nickname = pcall(function()
		return SaveData:RemoveAsync(key)
	end)
	if success then
		return true 
	end
end

local banned = readData("BannedPlayers")

game.Players.PlayerAdded:Connect(function(player)
	while true do
		wait(1)
		if player does action then
			table.insert(banned, player.UserId)
		else
			-- do nothing
		end

		if table.find(banned, player.UserId) then
			player:Kick("You have been banned.")
		else
			-- do nothing
		end
	end
end)
game.Players.PlayerRemoving:Connect(function(player) -- so that is works in the actual game
	local isSaved = saveData(banned, "BannedPlayers")
	if isSaved then
		print("Saved successfully!")
	end
end)
game:BindToClose(function() -- so that it works in roblox studio
	local isSaved = saveData(banned, "BannedPlayers")
	if isSaved then
		print("Saved successfully!")
	end
end)

Also, make sure that in the game settings, that Enable Studio Access to API Services is ON

Thanks for the reply, I appreciate it :smile: :+1:, could you explain how the script works?

Ok, so,

the functions use a pcall function so that it returns an error instead of crashing the entire script.

the banned uses datastores because it won’t automatically save.

in the PlayerRemoving event, it checks if it saves because each function returns a bool (true or false)

the game:BindToClose() function is there because if the game gets shutdown or you are trying to test it in studio, it will still save.

if you want an easier explanation, I recommend you check out these links.
https://developer.roblox.com/en-us/articles/Roblox-Client-Server-Model
https://developer.roblox.com/en-us/articles/Data-store