Need some help with hitboxes, specifically detection

I’ve made a simple sword weapon and now I’m working on the hitbox and detection part of things when the user swings.

I got this code running on the client, to clarify anim is a variable which has an animation and this runs and creates the hitbox, as intended.

On an unrelated note, I’m unsure if this is the best way of doing this part but it works and any advice is welcome and appreciated.

	anim:GetMarkerReachedSignal("Hit"):Connect(function()
		local hitbox = hitboxes.CreateHitbox(player.Character,Vector3.new(6,6,6),player.Character.HumanoidRootPart.CFrame * CFrame.new(Vector3.new(0,0,-3)))
		hitbox.Anchored = false
		local weld = Instance.new("WeldConstraint",hitbox)
		weld.Part0 = hitbox
		weld.Part1 = player.Character.HumanoidRootPart		

		delay(0.15, function()
			hitbox:Destroy()
		end)
	end)

Now I could simply use GetPartsBoundInBox() or GetPartsInPart() here and loop through the resulting table and do what needs to be done, for example dealing damage on all the characters, but I’m trying to create a lingering hitbox, one that would successfully detect a character and go through with this process even if they were to be ‘hit’ by it in the last moment of it’s lifetime (0.15s)

EDIT: For the record, hitboxes is a variable, CreateHitbox() is a function in the module that basically creates, sets up and returns the hitbox as a part.

What is the best way of going about this? Any feedback and help would be greatly appreciated.

3 Likes

I don’t know why you would need a hitbox that lasts 0.15 seconds. Isn’t a persistent one better?

You can

then check if there is any hitbox there yet, if not, create a hitbox and the weld and clone script that handles the destroy after 0.15 seconds.

1 Like

The hitbox lasts 0.15 seconds so that it ends in conjunction with the swing animation being played, wouldn’t want players to get after the animation ends after all.

The creation of the hitbox itself is fine, it gets the job done, what I’m trying to figure out is the best way to continuously detect everything touching the hitbox, it wouldn’t be as simple as a while true do loop right?

yea, maybe use RunService instead but that’s it.
Take it with a small grain of salt.

1 Like

RunService is likely the best option for running the actual loop since this is on the client side, but I’d still need to know how to actually set things up here, an example would be really nice. With the way I have things set up, every detected character should have a remote fire to the server to deal the actual damage with the damage system, among other things.

Yeah I definitely recommend using GetPartsBoundInBox thanks to its customization. Since it’s on the client you can just use RunService.

local character = game.Players.LocalPlayer.Character;
local root = character:WaitForChild("HumanoidRootPart");

local params = OverlapParams.new();
params.FilterType = Enum.RaycastFilterType.Include;
params.FilterDescendantsInstances = {character}; -- we immediately add our character so it doesn't get detected by the hitbox

local clock = os.clock();
local connection = nil;
connection = game:GetService("RunService").PostSimulation:Connect(function(dt)
   if os.clock() - clock >= 0.15 then -- if 0.15s have passed disconnect the event
      connection:Disconnect();
      return;
   end;
   local hitboxCFrame = root.CFrame * CFrame.new(0, 0, -3);
   local hitboxSize = Vector3.new(6, 6, 6);
   local parts = workspace:GetPartsBoundInBox(hitboxCFrame, hitboxSize, params);
   for _, part in ipairs(parts) do
      local hum = part.Parent:FindFirstChild("Humanoid");
      if hum == nil then continue end;
      params:AddToFilter(part.Parent); -- makes sure the enemy won't be hit multiple times
      CombatRemote:FireServer(part.Parent); -- replace with your remote
   end;
end);

Note that this would work on the server as well (besides the FireServer) but of course it would be way less responsive.
I wrote all this now without testing so if something errors let me know

1 Like

I’d like to express my utmost and sincerest gratitude as this exact code hasn’t solved the problem in my case but certain key parts of it have helped me construct a functional solution.

1 Like

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