Hitbox issue w/ Raycast Module

This issue is the final thing I need to solve to complete my project of making a combat system.

The issue: I’m using this module for raycasting, and basically, for some reason despite the hitbox hitting a player, it sometimes doesn’t “register” it, dealing no damage at all

Demo of my Weapon
tes - Roblox Studio (gyazo.com)

My Sword Code using the Module

local function Main(hit, enemyhumanoid)
	print('got hit')
	local humanoid = hit.Parent or hit.Parent.Parent
	local character = script.Parent.Parent

	if humanoid:FindFirstChildOfClass("Humanoid") and humanoid.Name ~= script.Parent.Parent.Name and humanoid:FindFirstChildOfClass("Humanoid").Health ~= 0 then

		--//Blocking//Check if Player is in Blocking Mode \\--
		if hit:FindFirstChild("IsASword") then -- Clash Effect regardless of blocking//both swinging

			if hit:FindFirstChild("IsSwinging").Value == true then

				local isBlocking = false

				for i, sword in pairs(hit.Parent:GetChildren()) do
					if sword:FindFirstChild("IsASword") then
						if sword:FindFirstChild("IsBlocking").Value == true then
							isBlocking = true
						end
					end
				end

				if isBlocking == false then

					cooldown = false

					local rayOrigin = hit.Position
					local rayDirection = Vector3.new(0, -100, 0)

					-- Build a "RaycastParams" object and cast the ray
					local raycastParams = RaycastParams.new()
					raycastParams.FilterDescendantsInstances = {hit.Parent}
					raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
					local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)



					Clash(hit, raycastResult.Position, character)

					if hit.Parent:FindFirstChildOfClass("Humanoid").Health <= 0  then
						local Tag = Instance.new("StringValue", hit.Parent)
						Tag.Name = 'PlayerKilled'
						--// How the Server will know which animation to Play\\--
						Tag.Value = tostring(script.Parent.DamageConfig.KillAnimation.AnimationId)
					end





					cooldown = true

				end

			end
		elseif hit and not hit:FindFirstChild("IsASword")  then
			local isBlocking = false

			for i, sword in pairs(hit.Parent:GetChildren()) do
				if sword:FindFirstChild("IsASword") then
					if sword:FindFirstChild("IsBlocking").Value == true then
						isBlocking = true
					end
				end
			end

			if isBlocking == true and userSwinging.Value == true then 
				local isFacingBack = false
				local range = 0.75


				local attacker = character:FindFirstChild("HumanoidRootPart")
				local target = hit.Parent:FindFirstChild("HumanoidRootPart")

				local Difference = (attacker.Position - target.Position).Unit
				local LookVector = target.CFrame.LookVector

				local DotProduct = Difference:Dot(LookVector)
			
				if DotProduct < 0 then
					isFacingBack = true
					--Player2 is facing completely opposite direction
				
					
				end



				if isFacingBack == true and debounceDamage == true then
					DamagePlrBack(hit, character)


				end
			end
		end

		coroutine.wrap(function()
			if Blocking.Value == false  then -- Player being attacked got hit (has no defense up) + Player is swinging
				
				
				
				DamagePlr(hit, character)

				if  hit:FindFirstChild("IsASword") and hit:FindFirstChild("IsSwinging").Value == false and hit:FindFirstChild("IsBlocking").Value == true then

					local rayOrigin = hit.Position
					local rayDirection = Vector3.new(0, -100, 0)

					-- Build a "RaycastParams" object and cast the ray
					local raycastParams = RaycastParams.new()
					raycastParams.FilterDescendantsInstances = {hit.Parent}
					raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
					local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)



					Clash(hit, raycastResult.Position, character)

				end
			end
		end)()
	end
	print('got time to finish')
end

hitbox.OnHit:Connect(function(hit, enemyhumanoid)
	Main(hit, enemyhumanoid)
end)

script.Parent.IsSwinging:GetPropertyChangedSignal("Value"):Connect(function()
	if script.Parent.IsSwinging.Value == true then
	
		hitbox:HitStart()
		hitbox:DebugMode(true)	
	else
		wait(1)
		hitbox:HitStop()
		
	end
end)
1 Like

"hitbox’ would be me referencing to the raycast module by the way, not sure how I would solve this hitbox issue where it doesn’t damage the player sometimes despite the hitbox hitting it (as you can see from the debug mode in the gif)

The method of using Rays in the first place can be pretty inconsistent. Instead, use Magnitude, region3, or just a .Touched event with a invisible part would be better option. You can use a the same script, I would just change the detection.