Help for a ban script

hello first welcome to my thread i need help with kicking the player so i can actually ban them but instead it kicks me instead of the targeted player when i use the command

local Admins = {1172975825,"BerkayTheCamper1"}
local Prefix = ";" 
local BanData = {}

function checkAdmin(player)
	for i,v in ipairs(Admins) do
		if type(v) == 'number' and player.UserId == v then
			return true
		elseif type(v) == 'string' and player.Name == v then
			return true
		end
	end
end
game.Players.PlayerAdded:Connect(function(plr)
			plr.Chatted:Connect(function(msg)
				local loweredString = string.lower(msg)
				local args = string.split(loweredString," ")
				if args[1] == Prefix.."ban" or args[1] == Prefix.."Ban" then
			for _,player in pairs(game:GetService("Players"):GetPlayers()) do						game:GetService("DataStoreService"):GetDataStore("BanStore"):SetAsync(args[2], BanData)
						player:Kick("Banned")
						end
					end
				end
			end)
end)

also i am new to scripting so no hate also i didnt find any threads about it

4 Likes

Your problem is the
for _, player in pairs(Game.Players:GetPlayers()
because you will have all players being kicked

2 Likes

how can i fix that then can you tell me please

3 Likes

Basically we will use args[2] to do this:


local Admins = {1172975825,"BerkayTheCamper1"}
local Prefix = ";" 
local BanData = {}

function checkAdmin(player)
	for i,v in ipairs(Admins) do
		if type(v) == 'number' and player.UserId == v then
			return true
		elseif type(v) == 'string' and player.Name == v then
			return true
		end
	end
end
game.Players.PlayerAdded:Connect(function(plr)
			plr.Chatted:Connect(function(msg)
				local loweredString = string.lower(msg)
				local args = string.split(loweredString," ")
				if args[1] == Prefix.."ban" or args[1] == Prefix.."Ban" then
			
						game:GetService("DataStoreService"):GetDataStore("BanStore"):SetAsync(args[2])
				
print(args[2])

game.Players:FindFirstChild(args[2]):Kick("Banned")
						
					end
				end
			end)
end)

I just don’t know if this is exactly working because I‘m not at home and I only have my Phone to write this.

2 Likes

let me try one second ill notify you if it works or not

1 Like

I accidently made it solution it doesnt work

1 Like

Ok I just rewrite the script so wait a second

1 Like

I found two errors:
what is the ban data by the DataStoreLine
and I found an error that I made sorry. (I fixed my error)

1 Like

Not sure why you have this part of the code as it will just kick everyone in the server.

2 Likes

I just said that in the first post but yeah

bro i am new to the scripting how would i know

1 Like

I just rewrote the script (if this also doesn’t work could you show the entire script?

It doesnt work it just says index nil with kick

1 Like

its the entire script already and i just have a unban script too under it but you dont have to see that

Can you show us what is now printed (with the new script) sorry but as I said I’m on mobile

The issue with the code you sent is that you are trying to find the player via the args 2 but the args 2 you are finding are all lowercase due to the string.lower(msg) and the user might have caps in there name.

1 Like

arguments 2 is just userid of the player

1 Like

Oh not the Name of the player? Ok

1 Like

Here is an edited version of your code. Should work.

I edited it to make it find the player in the correct way and also due to the way datastores work you need have it in a pcall function because sometimes it might not save to the datastore so we only want the user to be kicked if the user was successfully banned from the game (saved to the datastore)

local Admins = {1172975825,"BerkayTheCamper1"}
local Prefix = ";" 
local BanData = {}

function checkAdmin(player)
	for i,v in ipairs(Admins) do
		if type(v) == 'number' and player.UserId == v then
			return true
		elseif type(v) == 'string' and player.Name == v then
			return true
		end
	end
end


game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local loweredString = string.lower(msg)
		local args = string.split(loweredString," ")
		local Normal = string.split(msg, " ")
		if args[1] == Prefix.."ban" then
			game:GetService("DataStoreService"):GetDataStore("BanStore"):SetAsync(args[2], BanData)
			local success, errorMessage = pcall(function()
				game:GetService("DataStoreService"):GetDataStore("BanStore"):SetAsync(args[2], BanData)
			end)
			
			if success then
			if game:GetService("Players"):GetPlayerByUserId(args[2]) then
				game:GetService("Players"):GetPlayerByUserId(args[2]):Kick("Banned")
				end
			else
				print(errorMessage)
			end
		end
	end)
end)

You then also need to deal with the kick system when someone joins and they are banned!

1 Like

dont worry i have another script when they join if they are on data then they are banned