I have problems with my anti exploit

local DataStoreService = game:GetService("DataStoreService")
local BanPointsService = DataStoreService:GetDataStore("BanPointsData_0")

local Settings = {
	["Settings"] = {
		["WalkspeedLimit"] = 16;
		["JumpPowerLimit"] = 50;
		["AntiGodMode"] = true;
		["SpeedExploitValue"] = 20;
		["JumpPowerExploitValue"] = 25;
		["TeleportExploitValue"] = 20;
		["FireRemoteExploitValue"] = 75;
		["FlyExploitValue"] = 35;
	}
}

local Data = {
	["BanPoints"] = 0;
	["offense"] = "";
}


game.Players.PlayerAdded:Connect(function(plr)
	plr:SetAttribute("BanPoints",0)
	
	wait()
	local data
	data = Data
	local succes, errormessage = pcall(function()
		data = BanPointsService:GetAsync(plr.UserId)
	end)
	if succes == true then
		wait(1)
		plr:SetAttribute("BanPoints",data.BanPoints)
		Data = data
	else
		print('Error when loading')
		warn(errormessage)
	end
	
	if data.BanPoints >= 100 then
		plr:Kick("You are permently banned from this game!\n"..data.offense)
	end
	
	local function InspectCharacter()
	local Humanoid = plr.Character:WaitForChild("Humanoid")
	local HumanoidRootPart = plr.Character:WaitForChild("HumanoidRootPart")
	
	Humanoid.StateChanged:Connect(function(state)
			if state == Enum.HumanoidStateType.Flying or state == Enum.HumanoidStateType.StrafingNoPhysics or state == Enum.HumanoidStateType.PlatformStanding then
			plr:SetAttribute("BanPoints",plr:GetAttribute("BanPoints") - Settings.Settings.FlyExploitValue)
			Data.offense ..= "\n\n "..DateTime.now():FormatLocalTime("LLL","en-us").."- Fly exploit"
				plr:Kick("You were caught flying !"..DateTime.now():FormatLocalTime("L","en-us").."\n Points left: "..tonumber(100-plr:GetAttribute("BanPoints")).."\noffense list\n "..Data.offense)
		end
	end)
	
	Humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
		if Humanoid.WalkSpeed > Settings.Settings.WalkspeedLimit then
				plr:SetAttribute("BanPoints",plr:GetAttribute("BanPoints") - Settings.Settings.SpeedExploitValue)
				Data.offense ..= "\n\n "..DateTime.now():FormatLocalTime("LLL","en-us").."- Speed exploit"
				plr:Kick("You were caught speed exploiting!"..DateTime.now():FormatLocalTime("L","en-us").."\n Points left: "..tonumber(100-plr:GetAttribute("BanPoints")).."\noffense list\n "..Data.offense)
			end
		end)
		
	Humanoid:GetPropertyChangedSignal("JumpPower"):Connect(function()
		if Humanoid.JumpPower > Settings.Settings.JumpPowerLimit then
			plr:SetAttribute("BanPoints",plr:GetAttribute("BanPoints") - Settings.Settings.JumpPowerExploitValue)
			Data.offense ..= "\n\n "..DateTime.now():FormatLocalTime("LLL","en-us").."- JumpPower exploit"
				plr:Kick("You were caught JumpPower exploiting!"..DateTime.now():FormatLocalTime("L","en-us").."\n Points left: "..tonumber(100-plr:GetAttribute("BanPoints")).."\noffense list\n "..Data.offense)
		end
	end)
	end
if plr.Character then InspectCharacter() end
	plr.CharacterAdded:Connect(InspectCharacter)
end)


game.Players.PlayerRemoving:Connect(function(plr)
	Data.BanPoints = plr:GetAttribute("BanPoints")
	local data = Data
	local success, errormessage = pcall(function()
		BanPointsService:SetAsync(plr.UserId, data)
	end)
	if not success and errormessage then
		warn("Error when saving data : "..errormessage)
	end
end)

I got that

May anyone helps me?

I think you should save the player’s data before assuming it exists.

I just added or Data
data = BanPointsService:GetAsync(plr.UserId) or Data

it seems to work fine now ! I don’t know if it the best thing to do but , it works!

1 Like

Taking a look at your code, that seems to be the best way to do it, check the BanPointsService to see if the player has any existing BanPoints & if they do not then just use a default BanPoints table.