Ban Script error(DataSavingService)

Can someone tell me why my ban command isn’t working I get a nil error from the pcall that doesn’t make sense to me at all and last time I checked the datasavingservice is up
a551da51149c75b74683e1cd93714572
here’s the piece of the script that works the ban command don’t think you will need the entire script

	if caller:GetRankInGroup(000000) >= 0 then
		if banthisplayer then
			for i,v in pairs(game.Players:GetPlayers()) do -- loop through player list
				if string.lower(tostring(v)) == string.lower(banthisplayer) then --searching for player
					banthisplayer = v
					break
				end
			end
			if banthisplayer ~= tostring(banthisplayer) and banthisplayer:IsA("Player") then --making sure banthisplayer isn't a string and is a player
				local success,errormsg pcall(function()
					bannedlist:SetASync(banthisplayer.UserId)
				end)
				if success then
					print("Banned Successfully")
				else
					warn(errormsg)
				end
				banthisplayer:Kick("What have you done to be banned?")
			else
				print('"'..tostring(banthisplayer)..'"'.." Does Not Exist")
			end
		end
	else
		print("Your Rank isn't high enough")
	end
	
end

Any help will be appreciated

1 Like

:SetASync should be :SetAsync

1 Like

This was a problem but wasn’t the solution

use bannedlist:SetAsync(banthisplayer.UserId, true)

Why don’t just verify if the value bannedlist exist and is egual to true when player join with PlayerAdded ?

1 Like

This doesn’t work
also that is what I am doing unless I don’t understand what you are saying I’m kind of new to data saving

Ho, when, you can save in a DataStore the boolean true or whatever you want it’s just need to exist, after your verify in PlayerAdded if the player that joined has this data which exists, if it does not return “nil” that means that it exists and so in this case you kick the player. In the opposite case you do nothing. Here is a sample exemple of how use PlayerAdded

local Players = game:GetService("Players")

 Players.PlayerAdded:Connect(function(player) -- here player is the object of the player that just join the game
     local success, userValue = pcall(function()
		return bannedlist:GetAsync( [[PUT-HERE-THE-VALUE-KEY(Use userId )]] )
	end)
  if not userValue == nil then
  player:Kick("Ho, this is sad, you are banned >3")
  end
end)

I hope this will help you, i don’t tested it

*For unban user just delete the key of the player

1 Like

so if a
SetAsync makes the player equal true value(Meaning to kick it) then everytime a player joins check if the player has a true value if they don’t it will give off a error message from pcall it won’t kick them is this what you are saying?

I’m completely confused why this isn’t working so here is my whole script
and just need to fix it already any help will be appreciated

dataservice = game:GetService("DataStoreService")
bannedlist = dataservice:GetDataStore("BannedPlayers") 
local commands = {}
local prefix = ";"







--------------


commands.kick = function(caller,kickthisplr,reason)
	if caller:GetRankInGroup(7668499) >= 5 then
		if kickthisplr then
			print("Go")
			for i,v in pairs(game.Players:GetPlayers()) do
				if string.lower(v.Name) == string.lower(kickthisplr) then
					kickthisplr = v
					break
				end
			end
			if kickthisplr ~= tostring(kickthisplr) and kickthisplr:IsA("Player") then
				kickthisplr:Kick("You was kicked")
			else
				print('"'..kickthisplr..'"'.." Is not a existing player")
			end
		else
		end
	else
		print("Rank Isn't High Enough")
	end
end


commands.ban = function(caller,banthisplayer)
	if caller:GetRankInGroup(7668499) >= 0 then
		if banthisplayer then
			for i,v in pairs(game.Players:GetPlayers()) do -- loop through player list
				if string.lower(tostring(v)) == string.lower(banthisplayer) then --searching for player
					banthisplayer = v
					break
				end
			end
			if banthisplayer ~= tostring(banthisplayer) and banthisplayer:IsA("Player") then --making sure banthisplayer isn't a string and is a player
				local success,errormsg pcall(function()
					bannedlist:SetAsync(banthisplayer.UserId, "Your Banned")
				end)
				if success then
					print("Banned Successfully")
				else
					print(bannedlist:GetAsync(banthisplayer.UserId))
					warn(errormsg)
				end
				banthisplayer:Kick("What have you done to be banned?")
			else
				print('"'..tostring(banthisplayer)..'"'.." Does Not Exist")
			end
		end
	else
		print("Your Rank isn't high enough")
	end

end


--------------





game.Players.PlayerAdded:Connect(function(player)
	--checking if player is banned
	local success, errormsg = pcall(function()
		if bannedlist:GetAsync(player.UserId) then
			print("Banned")
		else
			print("Is not banned")
		end
	end)
	if success then
		print("debug")
	else
		warn(errormsg)
	end
	player.Chatted:connect(function(message,recipient)
		message = string.lower(message)
		local splitstring = message:split(" ")
		local slashcommand = splitstring[1]
		local cmd = slashcommand:split(prefix)
		local cmdname = cmd[2]
		if splitstring[2] and string.lower(splitstring[2]) == "me" then
			splitstring[2] = tostring(player)
		end
		if splitstring[3] and string.lower(splitstring[3]) == "me" then
			splitstring[3] = tostring(player)
		end
		if commands[cmdname] then
			commands[cmdname](player,splitstring[2],splitstring[3])
		else
			print("error")
		end
	end)
end)
dataservice = game:GetService("DataStoreService")
bannedlist = dataservice:GetDataStore("BannedPlayers") 
local commands = {}
local prefix = ";"


--------------


commands.kick = function(caller, ToBeKickedUsername, reason)
	if caller == nil or ToBeKickedUsername == nil then return end
	if caller:GetRankInGroup(7668499) >= 5 then
		for i,v in pairs(game.Players:GetPlayers()) do
			if string.find(v.Name, ToBeKickedUsername) then
				v:Kick(reason)
				break
			end
		end
	else
		print("Rank Isn't High Enough")
	end
end


commands.ban = function(caller, ToBeBannedUsername)
	if caller == nil or ToBeBannedUsername == nil then return end
	if caller:GetRankInGroup(7668499) >= 0 then
		for i,v in pairs(game.Players:GetPlayers()) do
			if string.find(v.Name, ToBeBannedUsername) then
				local success,errormsg pcall(function()
					bannedlist:SetAsync(v.UserId, "You are banned.")
				end)
				if success then
					v:Kick("You've been banned.")
					print("Banned Successfully")
				else
					print(bannedlist:GetAsync(v.UserId))
					warn(errormsg)
				end
				break
			end
		end
	else
		print("Your Rank isn't high enough")
	end
end


--------------


game.Players.PlayerAdded:Connect(function(player)
	--checking if player is banned
	local success, errormsg = pcall(function()
		local data = bannedlist:GetAsync(player.UserId)
		if data ~= nil then
			print("Banned")
			player:Kick(data)
		else
			print("Is not banned")
		end
	end)
	if success then
		print("debug")
	else
		warn(errormsg)
	end
	player.Chatted:connect(function(message,recipient)
		if message:sub(1,1) == prefix then
			message = string.lower(message)
			local splitstring = message:split(" ")
			local slashcommand = splitstring[1]
			local cmd = slashcommand:split(prefix)
			local cmdname = cmd[2]
			if splitstring[2] and string.lower(splitstring[2]) == "me" then
				splitstring[2] = tostring(player)
			end
			if splitstring[3] and string.lower(splitstring[3]) == "me" then
				splitstring[3] = tostring(player)
			end
			if commands[cmdname] then
				commands[cmdname](player,splitstring[2],splitstring[3])
			else
				print("error")
			end
		end
	end)
end)
3 Likes

Okay so while reading this and comparing it to my script I understood more now about the nil pretty confusing but thanks for this quick script also thank you @Med367367 as well for helping I appreciate it

Hello, @runaredie. Sorry i was sleeping x’)
Okay so, i know that your problem is solved but i will explain to you some things

Yes, and no. We set the value to true, but it could be anything as long as the value exists. For example, you can store the reason for the ban instead and retrieve it to put it in the Kick message

In fact, GetAsync will retrieve the data from Storage if it exists. If it does not find it / does not exist the function will return the value nil. In the current case, if the function return nil we do nothing because the person is not banned. In the opposite (the function returns what is saved and therefore not nil) case you kick the person

I hope this will be clearer for you

1 Like

Thank you very much for putting more effort into the post I appreciate it it is really clear now I have a bigger understanding of DataSavingService now due to you I appreciate your help