How can I kick a player if there's an error in a code?

Hi, I have made a banned zone kind of like “Kavra’s Kingdom”, but I want to make it so if there’s an error with the code it will kick them out. Like if there’s an error in my part and I’m not aware that it will kick them out saying there’s an error. Is there a post explaining how to do this?

This is currently the code I have.

local dataStore = dss:GetDataStore("GameXPData", 1)
local dataStore2 = dss:GetDataStore("PopUpsData", 1)
local lastPlaceData = dss:GetDataStore("LastLocation", 1)
local bannedPlayers = dss:GetDataStore("BannedUsers", 1)

game.Players.PlayerAdded:Connect(function(plr)
	local leaderboard = Instance.new("Folder", plr)
	leaderboard.Name = "leaderstats"

	local xp = Instance.new("NumberValue", leaderboard)
	xp.Name = "XP"
	xp.Value = 0

	local awardedXP = Instance.new("BoolValue", plr)
	awardedXP.Value = false
	awardedXP.Name = "AwardedXP"

	local workArea = Instance.new("StringValue", plr)
	workArea.Value = "None"
	workArea.Name = "WorkArea"

	local minutes = Instance.new("NumberValue", plr)
	minutes.Value = 0
	minutes.Name = "Minutes"

	local checkedShop = Instance.new("BoolValue", plr)
	checkedShop.Value = false
	checkedShop.Name = "CheckedShop"

	local checkedChannel = Instance.new("BoolValue", plr)
	checkedChannel.Value = false
	checkedChannel.Name = "CheckedChannel"

	local isBanned = Instance.new("BoolValue", plr)
	isBanned.Name = "IsBanned"
	isBanned.Value = false

	local timeBanned = Instance.new("NumberValue", plr)
	timeBanned.Name = "TimeBanned"
	timeBanned.Value = 0

	local banReason = Instance.new("StringValue", plr)
	banReason.Name = "BanReason"
	banReason.Value = "N/A"

	for i,v in pairs(game.Players:GetChildren()) do
		if v:IsA("NumberValue") or v:IsA("BoolValue") or v:IsA("StringValue") or v:IsA("IntValue") then
		local data
		local success, fail = pcall(function()
			data = dataStore:GetAsync(plr.UserId..v.Name)
		end)
		if success then
			print("Successfully loaded "..plr.Name.."'s "..v.Name.."'s value!")
			v.Value = data
			end
			end
	end
	
	
	wait(1)
	if isBanned.Value == false and banReason == "N/A" and timeBanned.Value == 0 then
		plr:Kick("[Auto Kick]: You are not banned, why did you try to join the Restricted Area? This area is restricted for banned players! DO NOT JOIN!")
		warn("Kicked "..plr.Name..", they're not banned!")
	end
local seconds = 60
game.ReplicatedStorage.Minutes.Value = timeBanned.Value
	repeat
		if seconds > 0 and timeBanned.Value >= 1 then
			seconds -= 1
			game.ReplicatedStorage.Minutes.Value = timeBanned.Value
			elseif seconds == 0 and timeBanned.Value >= 1 then
			seconds = 60
			timeBanned.Value -= 1
			game.ReplicatedStorage.Minutes.Value = timeBanned.Value
				
			
		end
		wait(1)
	until timeBanned.Value == 0 and seconds == 0
	
	isBanned.Value = false
	plr:Kick("[Auto Kick]: You have served your time! You have served your time, you are now unbanned! Please wait while we teleport you to the main game... \n\n If you aren't teleported within the next 15 seconds, please manually rejoin.")
	game:GetService("TeleportService"):Teleport(12073023350, plr)
	

end)

game.Players.PlayerRemoving:Connect(function(plr)
	for i,v in pairs(plr:GetChildren()) do
		if v:IsA("NumberValue") or v:IsA("BoolValue") or v:IsA("StringValue") or v:IsA("IntValue") then
			local success, fail = pcall(function()
				dataStore:SetAsync(plr.UserId..v.Name, v.Value)
			end)
			if success then
				print("Successfully saved "..plr.Name.."'s "..v.Name.."'s Data!")
			end
		end
	end
	for i,v in pairs(plr.leaderstats:GetChildren()) do
		local success, fail = pcall(function()
			dataStore:SetAsync(plr.UserId..v.Name, v.Value)
		end)
		end
end)

It’s kind of a mess.

You can use pcall() to detect errors.

Code
local dataStore = dss:GetDataStore("GameXPData", 1)
local dataStore2 = dss:GetDataStore("PopUpsData", 1)
local lastPlaceData = dss:GetDataStore("LastLocation", 1)
local bannedPlayers = dss:GetDataStore("BannedUsers", 1)

game.Players.PlayerAdded:Connect(function(plr)
	
	local suc,response = pcall(function()
		local leaderboard = Instance.new("Folder", plr)
		leaderboard.Name = "leaderstats"
	
		local xp = Instance.new("NumberValue", leaderboard)
		xp.Name = "XP"
		xp.Value = 0
	
		local awardedXP = Instance.new("BoolValue", plr)
		awardedXP.Value = false
		awardedXP.Name = "AwardedXP"
	
		local workArea = Instance.new("StringValue", plr)
		workArea.Value = "None"
		workArea.Name = "WorkArea"
	
		local minutes = Instance.new("NumberValue", plr)
		minutes.Value = 0
		minutes.Name = "Minutes"
	
		local checkedShop = Instance.new("BoolValue", plr)
		checkedShop.Value = false
		checkedShop.Name = "CheckedShop"
	
		local checkedChannel = Instance.new("BoolValue", plr)
		checkedChannel.Value = false
		checkedChannel.Name = "CheckedChannel"
	
		local isBanned = Instance.new("BoolValue", plr)
		isBanned.Name = "IsBanned"
		isBanned.Value = false
	
		local timeBanned = Instance.new("NumberValue", plr)
		timeBanned.Name = "TimeBanned"
		timeBanned.Value = 0
	
		local banReason = Instance.new("StringValue", plr)
		banReason.Name = "BanReason"
		banReason.Value = "N/A"
	
		for i,v in pairs(game.Players:GetChildren()) do
			if v:IsA("NumberValue") or v:IsA("BoolValue") or v:IsA("StringValue") or v:IsA("IntValue") then
			local data
			local success, fail = pcall(function()
				data = dataStore:GetAsync(plr.UserId..v.Name)
			end)
			if success then
				print("Successfully loaded "..plr.Name.."'s "..v.Name.."'s value!")
				v.Value = data
				end
				end
		end
		
		
		wait(1)
		if isBanned.Value == false and banReason == "N/A" and timeBanned.Value == 0 then
			plr:Kick("[Auto Kick]: You are not banned, why did you try to join the Restricted Area? This area is restricted for banned players! DO NOT JOIN!")
			warn("Kicked "..plr.Name..", they're not banned!")
		end
	local seconds = 60
	game.ReplicatedStorage.Minutes.Value = timeBanned.Value
		repeat
			if seconds > 0 and timeBanned.Value >= 1 then
				seconds -= 1
				game.ReplicatedStorage.Minutes.Value = timeBanned.Value
				elseif seconds == 0 and timeBanned.Value >= 1 then
				seconds = 60
				timeBanned.Value -= 1
				game.ReplicatedStorage.Minutes.Value = timeBanned.Value
					
				
			end
			wait(1)
		until timeBanned.Value == 0 and seconds == 0
		
		isBanned.Value = false
		plr:Kick("[Auto Kick]: You have served your time! You have served your time, you are now unbanned! Please wait while we teleport you to the main game... \n\n If you aren't teleported within the next 15 seconds, please manually rejoin.")
		game:GetService("TeleportService"):Teleport(12073023350, plr)
		
	end)

	if not (suc) then
		plr:Kick("There was an error, so you got kicked.")
		error(response)
	end

end)

game.Players.PlayerRemoving:Connect(function(plr)
	for i,v in pairs(plr:GetChildren()) do
		if v:IsA("NumberValue") or v:IsA("BoolValue") or v:IsA("StringValue") or v:IsA("IntValue") then
			local success, fail = pcall(function()
				dataStore:SetAsync(plr.UserId..v.Name, v.Value)
			end)
			if success then
				print("Successfully saved "..plr.Name.."'s "..v.Name.."'s Data!")
			end
		end
	end
	for i,v in pairs(plr.leaderstats:GetChildren()) do
		local success, fail = pcall(function()
			dataStore:SetAsync(plr.UserId..v.Name, v.Value)
		end)
		end
end)
1 Like

You cannot do this on server as LogService is client only, you can use pcall() for any unsuccessful functions or call.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.