How to dictate how long a hitbox lasts?

So i was trying to make a hitbox for an ability, whereby the hitbox is welded to the player and follows the player for a set amount of time, unfortunately I have no idea how to dicate how long it lasts for or if its welded correctly

local rs = game:GetService("ReplicatedStorage")
local stevent = rs:WaitForChild("SkillsEvent").STEvent


stevent.OnServerEvent:Connect(function(plr)
	local chr = plr.Character
	chr:SetAttribute("IsAttacking", true)
	local hrp = chr.PrimaryPart
	local rootpart = chr.PrimaryPart
	local bv = Instance.new("BodyVelocity")
	bv.MaxForce = Vector3.new(math.huge, 0, math.huge)
	bv.Velocity = rootpart.CFrame.LookVector * 50
	bv.Parent = rootpart
	local Watertrial = rs:WaitForChild("Modules").WeaponHander.WaterTrail
	local ListOfWeapons = {
		"BasicSword",
		"RengokuSword",
		"PlaceHolder1",
	}
	
	for i,v in chr:GetChildren() do
		if v:IsA("MeshPart") and table.find(ListOfWeapons, v.Name) then
			for i,s in Watertrial:GetChildren() do
				local ClonedPart = s:Clone()
				ClonedPart.Parent = v
				ClonedPart.Attachment0 = v.Attachment1
				ClonedPart.Attachment1 = v.Attachment2
			end
		end
	end
	
	local hitboxCFrame = chr.HumanoidRootPart.CFrame
	local hitboxSize = Vector3.new(10,5,20)
	local damage = 18
	local hitbox = rs.Hitbox2:Clone()
	hitbox.Parent = workspace
	hitbox.CFrame = hitboxCFrame
	hitbox.Size = Vector3.new(hitboxSize, hitboxSize, hitboxSize)
	local weld = Instance.new("WeldConstraint")
	weld.Part0 = hitbox
	weld.Part1 = plr.Character:WaitForChild("Torso")
	weld.Parent = hitbox
	local hitcontent = workspace:GetPartBoundsInBox(hitboxCFrame, hitboxSize)
	local hitlist = {}
	for _,v in pairs(hitcontent) do
		if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= chr and not table.find(hitlist, v.Parent) then
			table.insert(hitlist, v.Parent)
			v.Parent.Humanoid:TakeDamage(damage)
		end
	end

	task.wait(1)
	for i,v in chr:GetChildren() do
		if v:IsA("MeshPart") and table.find(ListOfWeapons, v.Name) then
			v.Trail:Destroy()
		end
	end
	bv:Destroy()
	chr:SetAttribute("IsAttacking", false)

	
end)


2 Likes

For starters, you’re welding the part directly onto the Torso meaning the hitbox center point will be at the center point of the Torso. If your hitbox is long it’s not going to behave like you’d want to. You can manipulate the weld’s C0 and C1 properties based on the hitbox size to fix this.

Secondly, to make it last longer you can just use a while loop

local currentTime = tick()
local hitlist = {} -- we keep this outside so the hitbox doesn't hit an opponent more than once
while true do
   if tick() - currentTime >= 1 then break end
   -- -- --
   local hitcontent = workspace:GetPartBoundsInBox(hitboxCFrame, hitboxSize)
   for _,v in pairs(hitcontent) do
		if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= chr and not table.find(hitlist, v.Parent) then
			table.insert(hitlist, v.Parent)
			v.Parent.Humanoid:TakeDamage(damage)
		end
   end
   -- -- --
   task.wait() -- you can replace this with RunService.Heartbeat/RunService.PostSimulation but it's the same thing
end

Also, you can get rid of v.Parent ~= chr by just putting chr inside the hitlist table.
Hope this helps

*You might also have to replace hitboxCFrame with hitbox.CFrame so that it updates as it moves, unless you want it to use the same cframe everytime

1 Like

They’re using a WeldConstraint not a Weld, meaning it will retain it’s original offset when part0 and 1 is set.

You could also use overlap params for better readability instead of putting the character inside the hitlist. However both options would work fine

Oh yeah I didn’t notice oopsies

Reading the code again he isn’t using GetPartsInPart neither which makes me wonder why he’s using a part in the first place. Seems to go completely unusued, unless perhaps it’s for debugging purposes

1 Like

How would i do this? i changed it to get parts in part but it returns an overlap error

You can just change your hitcontent variable to this:
local hitcontent = workspace:GetPartsInPart(hitbox)