How do I make a M1 Hitbox affect multiple players using :GetPartsInPart?

Every discussion i’ve seen on here has been using .Touched for their solutions to this problem… I don’t know if it’s the same for :GetPartsInPart… but I’ve tried using the table solution and that doesn’t seem to work. Here is the code that damages a player in a hitbox (sorry if its messy):

task.spawn(function()
		local overlapParams = OverlapParams.new()
		overlapParams.FilterType = Enum.RaycastFilterType.Exclude
		overlapParams.FilterDescendantsInstances = {char}

		local hitBoxParts = workspace:GetPartsInPart(hitbox, overlapParams)

		for _, parts in pairs(hitBoxParts) do
			
			if parts.Parent:FindFirstChildOfClass("Humanoid") then
				if debounce == false then
					
					debounce = true
					
					parts.Parent.Humanoid.WalkSpeed = 1
					
					local highlight = Instance.new("Highlight")
					highlight.Parent = parts.Parent
					highlight.OutlineTransparency = 1
					highlight.FillTransparency = 0.6
					highlight.FillColor = Color3.new(1, 1, 1)
					highlight.OutlineColor = Color3.new(1,1,1)
					highlight.OutlineTransparency = 0.6
					
					local function highlightTweenOut()
						task.spawn(function()
							task.wait(0.2)
							ts:Create(highlight, TweenInfo.new(0.2), {
								FillTransparency = 1,
								OutlineTransparency = 1
							}):Play()
							highlight:Destroy()
						end)
					end
					

					parts.Parent.Torso.Hit:Play()

					parts.Parent.Humanoid:TakeDamage(5)
					highlightTweenOut()

					parts.Parent.Humanoid.Died:Connect(function()
						parts.Parent.Torso.KnockOut:Play()
					end)


					if m1Anim == 4 and parts.Parent:FindFirstChild("HumanoidRootPart") then
						parts.Parent.Torso.HeavyHit:Play()

						local origRot = parts.Parent.HumanoidRootPart.CFrame.Rotation
						local origX = char.HumanoidRootPart.CFrame.Position.X

						ts:Create(parts.Parent.HumanoidRootPart, TweenInfo.new(0.2, Enum.EasingStyle.Quad), {
							["CFrame"] = char.HumanoidRootPart.CFrame * CFrame.new(0, 0, -30)
						}):Play()

						parts.Parent.Humanoid.PlatformStand = true
						parts.Parent.Humanoid.WalkSpeed = 0

						task.wait(2)

						parts.Parent.Humanoid.WalkSpeed = 20
						parts.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
						parts.Parent.Humanoid.PlatformStand = false
					end

				end
			end
		end
		
		task.wait(0.2)
		debounce = false
	end)
1 Like

You should utilize :GetPartBoundsInBox() instead of :GetPartsInPart(). Its essentially the same thing, just instead of a part being used, you are using an imaginary Box.

To store each individual character that has been located inside of the box, you can have a table that stores the Character Model of each targeted individual.

local hitBoxParts = workspace:GetPartBoundsInBox(CFrame, Size, Params)
local targets = {}

for _, parts in pairs(hitBoxParts) do
    if parts.Parent:FindFirstChildOfClass("Humanoid") and not table.find(targets, parts.Parent) then 
           table.insert(targets, parts.Parent)
           -- Other code
    end
end

After that, you can do whatever with the targets table.

1 Like

Instead of using :GetPartsInPart you can use GetBoundsInBox, This will affect multiple players.

local function meleeAttack(position, range)
   -- Define the size of the attack area
   local size = Vector3.new(range, range, range)
   local box = CFrame.new(position) * CFrame.new(0, 0, 0)

   -- Get all parts within the defined area
   local parts = Workspace:GetPartsInBox(box, size)

   -- Loop through all parts found in the box
   for _, part in ipairs(parts) do
       local character = Players:GetPlayerFromCharacter(part.Parent)

       if character then
           -- Apply damage or any effect you want
           local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
           if humanoid then
               humanoid:TakeDamage(10) -- Adjust the damage value as needed
           end
       end
   end
end

Thanks for responding, I tried this… does the same thing as it did when I used the GetPartsInPart, which makes me think im just putting this table in the incorrect spots… :sob:

Updated Code:

local boxPos = char.HumanoidRootPart.CFrame * CFrame.new(0,0,-5)
		local hitBoxParts = workspace:GetPartBoundsInBox(boxPos, Vector3.new(6,6,6) , overlapParams)
		local targets = {}

		for _, parts in pairs(hitBoxParts) do
			
			if parts.Parent:FindFirstChildOfClass("Humanoid") and not table.find(targets, parts.Parent) then
				table.insert(targets, parts.Parent)
				
				if debounce == false then
					
					debounce = true
					
					parts.Parent.Humanoid.WalkSpeed = 1
					
					local highlight = Instance.new("Highlight")
					highlight.Parent = parts.Parent
					highlight.OutlineTransparency = 1
					highlight.FillTransparency = 0.6
					highlight.FillColor = Color3.new(1, 1, 1)
					highlight.OutlineColor = Color3.new(1,1,1)
					highlight.OutlineTransparency = 0.6
					
					local function highlightTweenOut()
						task.spawn(function()
							task.wait(0.2)
							ts:Create(highlight, TweenInfo.new(0.2), {
								FillTransparency = 1,
								OutlineTransparency = 1
							}):Play()
							highlight:Destroy()
						end)
					end
					

					parts.Parent.Torso.Hit:Play()

					parts.Parent.Humanoid:TakeDamage(5)
					highlightTweenOut()

					parts.Parent.Humanoid.Died:Connect(function()
						parts.Parent.Torso.KnockOut:Play()
					end)


					if m1Anim == 4 and parts.Parent:FindFirstChild("HumanoidRootPart") then
						parts.Parent.Torso.HeavyHit:Play()

						local origRot = parts.Parent.HumanoidRootPart.CFrame.Rotation
						local origX = char.HumanoidRootPart.CFrame.Position.X

						ts:Create(parts.Parent.HumanoidRootPart, TweenInfo.new(0.2, Enum.EasingStyle.Quad), {
							["CFrame"] = char.HumanoidRootPart.CFrame * CFrame.new(0, 0, -30)
						}):Play()

						parts.Parent.Humanoid.PlatformStand = true
						parts.Parent.Humanoid.WalkSpeed = 0

						task.wait(2)

						parts.Parent.Humanoid.WalkSpeed = 20
						parts.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
						parts.Parent.Humanoid.PlatformStand = false
					end

				end
			end
		end

So when I tested the code, it worked fine. When I included the debounce however, it started to not work when targeting multiple characters. I believe your debounce is the thing causing the issue.

Move the debounce check outside of the for loop and near the top of when the information is being sent to the server.

if debounce then return end
debounce = true

-- Do punching code
-- (the stuff you already have, without the if statement checking the debounce)

-- After the for loop control the debounce
task.wait(0.2)
debounce = false
1 Like

thanks, I totally forgot to remove the debounce from the script

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