Script taking 0.018 seconds to retrieve the enemy in my hitbox system

  1. Essentially, I’d like to figure out the reason for my script taking so long

  2. In the hitbox system, I use spatial query in a run service loop to get a table of the items within the hitbox. Then id fire the function (“maury”) that sorts the items within the table to retrieve the closest enemy.

Now the problem is, enemy would be “nil” if it doesnt wait 0.018 seconds.
So here is the code;

-----------MAURY FUNCTION

-------HITBOX SYSTEM


I return the enemy so that I can use it in another script.
There are 2 images to that hitbox system incase one doesnt load

  1. Another problem that occurred was the hitbox still querying after the object was destroyed. I had to use the “stop” boolean to solve it, however I’d still like to know why it was happening.

  2. Im assuming this hitbox system is affecting my games FPS because I’m not sure what else it could be.

  3. Overall, any constructive criticism is appreciated. I’d like to improve as as scripter.

Thanks

1 Like

It might be because you put the code that sets the table after a RunService.Heartbeat already ran, which is why it would be 0.018 seconds. If you could paste your code, I can fix it for you.

Also overall, your scripts seem pretty good, maybe some more organization and a consistent naming scheme for readability but other than that it’s good.

Interesting, it’s 2am so in a couple hours I’ll send the code. Really appreciate it. & Thanks, I’d always wonder if I was doing something wrong when coding or if there’s more optimization I could be doing.

local hrp = char.HumanoidRootPart
local roota = hrp.RootAttachment
local hum = char.Humanoid
local at = char.Humanoid.Animator
local plr = game.Players:WaitForChild(char.Name)

local enemy

local stop = false
local connection


if  not plr then return end
local combat_settings = CombatSettings.GetSettings(char)
---------------------------------------Hitbox--------------------------------------------
local hitbox = hitbox_stuff:WaitForChild(name):Clone()
if cf_adjust then
	hitbox.CFrame = hrp.CFrame * CFrame.new(0,1.5,-3) --* CFrame.Angles(math.rad(90),0,math.rad(0))
else
	hitbox.CFrame = hrp.CFrame * CFrame.new(0,1.5,0) --* CFrame.Angles(math.rad(90),0,math.rad(0))

end
hitbox.Parent = workspace
hitbox.Anchored = false
local cf_adjust = CFrame.new(0,0,4)
weld(hitbox, hrp, cf_adjust, "box")

task.delay(0.025, function()
	if hitbox:WaitForChild("box") then
		local box = hitbox:WaitForChild("box")
		box:Destroy()
		hitbox.Anchored = true
		task.delay(0.09, function()
			hitbox:Destroy()
			stop = true
			connection:Disconnect()
		end)
	end
end)
---------------------------------------Spatial Query--------------------------------------------
local params = OverlapParams.new()
params.FilterType = Enum.RaycastFilterType.Whitelist
local tabl = workspace.LivingThings:GetChildren()
local tchar = table.find(tabl, char)
table.remove(tabl, tchar)
params.FilterDescendantsInstances = {tabl}

local touch_table = {}


connection = RS.Heartbeat:Connect(function()
	if stop then return end
	if not hitbox then return end
	if #touch_table >= 1 then
		--so if there is moree than one item inside the touch table then we want to fire a function to sort out
		--who the parents are
		connection:Disconnect()
		stop = true
		----------------Retrieve Enemy
		enemy = maury(hrp, touch_table)
		print(enemy)
		return 
	end
	touch_table =  workspace:GetPartsInPart(hitbox, params)
end)

task.wait(0.018)
return enemy

end

1 Like

For your main problem, try moving the GetPartsInPart line at the bottom of the Heartbeat to the top of the Heartbeat function, this should make it query at the right time since the Heartbeat function takes roughly 0.0167 seconds to loop, which explains why you would need the task.wait.

For the hitbox querying after the object was destroyed, are you sure that it’s not because the table wasn’t cleared? The touch_table still exists after the hitbox is destroyed.

So I should move the “GetPartsInPart” to the top of the heartbeat function? Then wouldnt that mean it could potentially fire it again despite the touchtable being returned.

Essentially what I’m asking is, would it be able to override the previous touch table?

Sorry for the late response, I didn’t see your reply until now.

Fixed code:

local hrp = char.HumanoidRootPart
local roota = hrp.RootAttachment
local hum = char.Humanoid
local at = char.Humanoid.Animator
local plr = game.Players:WaitForChild(char.Name)

local enemy

local stop = false
local connection

if  not plr then return end

local combat_settings = CombatSettings.GetSettings(char)
---------------------------------------Hitbox--------------------------------------------
local hitbox = hitbox_stuff:WaitForChild(name):Clone()

if cf_adjust then
	hitbox.CFrame = hrp.CFrame * CFrame.new(0,1.5,-3) --* CFrame.Angles(math.rad(90),0,math.rad(0))
else
	hitbox.CFrame = hrp.CFrame * CFrame.new(0,1.5,0) --* CFrame.Angles(math.rad(90),0,math.rad(0))

end

hitbox.Anchored = false
hitbox.Parent = workspace

local cf_adjust = CFrame.new(0,0,4)
weld(hitbox, hrp, cf_adjust, "box")

task.delay(0.025, function()
	if hitbox:WaitForChild("box") then
		local box = hitbox:WaitForChild("box")
		box:Destroy()
		hitbox.Anchored = true
		task.delay(0.09, function()
			hitbox:Destroy()
			stop = true
			connection:Disconnect()
		end)
	end
end)
---------------------------------------Spatial Query--------------------------------------------
local params = OverlapParams.new()
params.FilterType = Enum.RaycastFilterType.Whitelist

local tabl = workspace.LivingThings:GetChildren()

local tchar = table.find(tabl, char)
table.remove(tabl, tchar)

params.FilterDescendantsInstances = {tabl}

local touch_table = {}

connection = RS.Heartbeat:Connect(function()
	if stop or not hitbox then 
		return 
	end
	
	touch_table =  workspace:GetPartsInPart(hitbox, params)

	if #touch_table >= 1 then
		--so if there is moree than one item inside the touch table then we want to fire a function to sort out
		--who the parents are
		connection:Disconnect()
		stop = true
		----------------Retrieve Enemy
		enemy = maury(hrp, touch_table)
		print(enemy)
		return 
	end
end)

return enemy

mmm ok thanks a lot. I get what you meant now

I tried it and 2 problems occurred.

1; The query fires 2 times.

2; It still required the task.wait in order to return the enemy.

So, I’m probably going to use a fast-cast like system and transfer the information from the client to the server and back to the client. Im not sure though.

Someone recommended something called; parallel luau. So i’ll check that out.

Thanks for the feedback