Kick system with barriers to avoid glitchers

I’ve made this system that detects if a player is inside of a part, and if they are then kick them, but it’s a bit laggy. How can i improve it and keep it short??

function module.activateBarriers()
	local map = game.Workspace.Assets:FindFirstChild("Map")
	if not map then
		return
	end
	local barriers = map:FindFirstChild("Barriers")
	if not barriers then
		return
	end
	task.spawn(function()
		while wait() do
			if #barriers:GetChildren() < 1 then
				return
			end
			for i, barrier in pairs(barriers:GetChildren()) do
				if barrier.Name ~= "Barrier" and not game.Workspace.Assets.Doors.Locked:FindFirstChild(barrier.Name) then
					barrier:Destroy()
				else
					local results = workspace:GetPartsInPart(barrier)
					if results then
						for i, part in pairs(results) do
							if part.Name == "HumanoidRootPart" then
								local character = part.Parent
								local player = players:GetPlayerFromCharacter(character)
								if character and player then
									if roundInfo.Players:FindFirstChild(player.Name) then
										player:Kick("Out of bounds")
									end
								end
							end
						end
					end
				end
				task.wait(.05)
			end
		end
	end)
end

This is bad practice, use task.wait() or RunService.Heartbeat:Wait()

i changed the system to use task.wait(), and now it loops through the players and checks if they’re touching a barrier

much more efficient and if I destroy a barrier then I don’t have to account for it still being in a loop!

1 Like