My script works properly but, if there are any other efficient way more than this

local Part = game.Workspace.Outer
local Baseplate = game.Workspace.Baseplate
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

local function OverlapParam()
	while task.wait() do
		local OverlapParam = OverlapParams.new()
		OverlapParam.FilterDescendantsInstances = {Part,Baseplate}
		OverlapParam.FilterType = Enum.RaycastFilterType.Blacklist


		local Thing = game.Workspace:GetPartsInPart(Part,OverlapParam)
		return Thing
	end
end

while true do
	task.wait(1)
	local Table = {}
	
	for i,v in pairs (OverlapParam()) do
		
		for i,Char in pairs (Character:GetChildren()) do
			if Char:IsA("BasePart") then
				if not table.find(Table,Char) and Char then
					table.insert(Table,Char)
				end
			
			end
		end
		
		for hi,TableProperties in pairs(Table) do
			if v == TableProperties then
				Humanoid.Health = 0
                return
			else
               v:Destroy()
			end
		end
	end
end

Recently im learning OverlapParams. This script works properly, but is there any efficient way else from this? Can you tell me, if you see any error or mistakes? Thanks.

May be better for the #help-and-feedback:code-review section, but yes there are quite a few things that can be done better. First off since your OverlapParam settings do not change you should declare them globally like the rest of your references to Part, BasePart, Character.

The same function has a flaw in it where I think you are assuming it won’t return if it’s empty? Either way it’s in a while loop for no reason because it will return on the first run, there is no if/else to divert it from leaving immediately. This also makes the task.wait at the start run for no reason, sadly delaying your scripts by a frame.

Suddenly this first function is rather simple:

local OverlapParam = OverlapParams.new()
OverlapParam.FilterDescendantsInstances = {Part,Baseplate}
OverlapParam.FilterType = Enum.RaycastFilterType.Blacklist

local function OverlapParam()
	return game.Workspace:GetPartsInPart(Part,OverlapParam)
end

The main part of your script inside the while loop can do away with a lot of unnecessary variable creation and checking. If we only care to find a player’s character/humanoid inside the OverlapParam() we do not need to find every child in the character, instead just checking the parent of each item, if it’s the character then we are done!

for i,v in pairs (OverlapParam()) do
    if v.Parent == Character then
        Humanoid.Health = 0
        return
    else
        v:Destroy() -- this seems excessive
    end
end

All and all your script looks like this

local Part = game.Workspace.Outer
local Baseplate = game.Workspace.Baseplate
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

local OverlapParam = OverlapParams.new()
OverlapParam.FilterDescendantsInstances = {Part,Baseplate}
OverlapParam.FilterType = Enum.RaycastFilterType.Blacklist

local function OverlapParam()
	return game.Workspace:GetPartsInPart(Part,OverlapParam)
end

while task.wait(1) do
	for i,v in pairs (OverlapParam()) do
	    if v.Parent == Character then
	        Humanoid.Health = 0
	        return
	    else
   	        v:Destroy()
	    end
	end
end
2 Likes

Thanks for help, i understood.