Need help to find out why my hitboxes don't work sometimes

I am currently trying to make a general hitbox system for my game and well it’s not going as planned. The hitboxes do work when they actually want to. However, sometimes they just fail to register anything at times.

As you can see the hitboxes sometimes work and don’t. Is it because I’m using the touched event? Is it the way my code is structured?

Below is the function in a module script I created to spawn the hitboxes. Maybe I’m doing it wrong or there is something obvious going over my head but I genuinely can’t see it as of now. Spawning in the hitbox isn’t the problem but the detection if it hits a player is.

function SkillsHandler.spawnHitBox(player, character, rootpart)
	local humanoid = character.Humanoid
	local Hitbox = Hitboxes[SkillsHandler.AnimName]:Clone()
	local Heartbeat
	
	Hitbox.CFrame = rootpart.CFrame * SkillsHandler.CFrame
	Hitbox.Parent = workspace.hitboxes
	
	local weld = Instance.new("Weld")
	weld.Part0 = Hitbox
	weld.Part1 = rootpart
	weld.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
	weld.Parent = weld.Part0
	
	local connection = Hitbox.Touched:Connect(function() end)
	
	Heartbeat = RunService.Heartbeat:Connect(function()
		local results = Hitbox:GetTouchingParts()
		task.wait()
		
		coroutine.wrap(function()
			for i=1, #results do
				local obj = results[i]

				--if obj we hit is not part of our character then continue
				if not obj:IsDescendantOf(character) then
					local enemyHum = obj.Parent:FindFirstChild("Humanoid")
					local enemyRoot = obj.Parent:FindFirstChild("HumanoidRootPart")

					--if player is NOT dead and valid objects then continue
					if enemyHum and enemyRoot and enemyHum.Health > 0 then
						if SkillsHandler.ConfirmTarget[player.Name] then
							return nil
						end

						SkillsHandler.ConfirmTarget[player.Name] = true

						local targetAnim = SkillsHandler.playAnimations(enemyHum, "Target")
						targetAnim:Play()

						--insert dmg formula
						coroutine.wrap(function()
							DamageFormula.dmgCalc(player, character, humanoid, enemyHum)

						end)()

						if Hitbox then
							Hitbox:Destroy()
						end
						
						if connection:Disconnect() then
							
						end

						if Heartbeat then
							Heartbeat:Disconnect()
						end
						return
					end
				end
			end
		end)()
	end)
	
	task.delay(SkillsHandler.hitboxDuration, function()
		if Hitbox then
			Hitbox:Destroy()
		end
		
		if connection:Disconnect() then

		end
		
		if Heartbeat then
			Heartbeat:Disconnect()
		end
	end)
end

return SkillsHandler

What I have found is that when I move character more then use the second move it will connect most times. However, even then it still fails at times and if I stand still like in the video it guaranteed does that.

Try using GetPartsInPart instead of GetTouchingParts:

GetPartsInPart() - Roblox Creator Documentation

Otherwise I recommend placing prints() in your code so you can actually know where it is not detecting being touched.

So I did some printing and found the issue. The issue was the if statement for skillhandler.confirmTarget. I just removed it since it’s unnecessary and the server already does a check before using the module. Now everything works fine. Thank you for the advice.

1 Like