How to make raycast only hit humanoid

As the title says I want to make raycasts only hit humanoid

I Can’t add everything to the blacklist so I need another way


local db = false


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RaycastHitbox = require(ReplicatedStorage.RaycastHitboxV4)

local Params = RaycastParams.new()

Params.FilterType = Enum.RaycastFilterType.Blacklist
Params.FilterDescendantsInstances = {script.Parent.Parent.Parent.Character, script.Parent.Handle}
local canHit = false
local RS = game:GetService("ReplicatedStorage")
local randomSlash = 1

local idle 
local slash
local equip

local con = {
	trailEnabled = true;
	bloodEffects = true
}


script.Parent.Equipped:Connect(function()
	script.Parent.Handle.Equip:Play()
	local Humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
	
	

end)
local newHitbox = RaycastHitbox.new(script.Parent.Handle)
newHitbox.RaycastParams = Params 

script.Parent.Activated:Connect(function()
	if not db then
		newHitbox:HitStart()
		db = true
		if con.trailEnabled == true then
			script.Parent.Handle.Trail.Enabled = true
		end
		local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
		if not slash then
			if randomSlash == 1 then
				slash = humanoid:LoadAnimation(RS.Slash1)
				randomSlash = randomSlash + 1
			elseif randomSlash == 2 then
				slash = humanoid:LoadAnimation(RS.Slash2)
				randomSlash = randomSlash - 1
			end
		end
		slash:Play()
		wait(.2)
		script.Parent.Handle.Slash:Play()
		wait(.8)
		if slash then
			slash:Stop()
			slash = nil
		end
		script.Parent.Handle.Trail.Enabled = false
		db = false
	end
end)



newHitbox.OnHit:Connect(function(hit, humanoid)
	print(hit, hit.Parent)
	if db then
		
			local effect = script.Parent.Handle.Effect:Clone()
			if hit.Parent:FindFirstChild("Humanoid") then
				local hitSFX = Instance.new("Sound",hit.Parent.Head); hitSFX.SoundId = "rbxassetid://566593606"; hitSFX:Play()
				humanoid:TakeDamage(50)
				if con.bloodEffects == true then
					effect.Parent = hit.Parent.HumanoidRootPart
					effect.Enabled = true
			elseif hit.Parent.Sword.Handle then
				script.Parent.Handle.swordClash:Play()
			end
			wait(2)
			effect:Destroy()
			canHit = false
			newHitbox:HitStop()
		end
	end
end)




script.Parent.Unequipped:Connect(function()
	if slash then slash:Stop() end
end)
1 Like

Instead of a backlist, use a whitelist and add the parts you want to check intersections with to it.

1 Like

If I want to get the humanoid then I would need to do hit.Parent.Humanoid but I can do that in the hit function

Sorry, I don’t understand the problem. It looks like the module you’re using provides the humanoid for you already in the OnHit event.

Yes but I can only use it inside the hit function newHitbox.OnHit:Connect(function(hit, humanoid) if I could use hit and humanoid outside of it then I wouldn’t have a problem

I don’t think you’re being entirely clear here in your post, when you state that

I want to make raycasts only hit humanoid

Since humanoids are not baseparts, there isn’t any possibility for a ray to overlap it, do you instead mean that you only want the ray to overlap with descendants of any model with a humanoid in it?

I guess, I just want the humanoid to take damage when hit with the sword

You have to index a humanoid based on a part in a character, there’s no way around it.

I’m sorry but I don’t get what you mean could you explain.

I’m not entirely familiar with the module you’re working with, but I assume if it returns a RaycastResult object then you can use the result.Instance property to traverse up and check if a humanoid exists

--result is the raycastresult object
local inst = result.Instance
if inst and inst.Parent then
    local humanoid = inst:FindFirstChildOfClass("Humanoid")
    if humanoid then
        --there is a humanoid, do what you want with it (like dealing damage)
    else
        --there isn't a humanoid in the same scope as the part that was hit
    end
end

As one of the previous replies says, humanoid is not a BasePart meaning you can not directly index it from the raycast result. You will have to use .Parent functions and if statements to index a humanoid. There’s no other way to do it.

Would there be a better way to make a sword instead of raycasts?

Raycasting is a somewhat unusual way to do collision detection for swords, not to say that it’s a bad idea (I’ve seen some really great examples of weapons using raycasting) but I wouldn’t exactly recommend it to people who’d consider themselves to be somewhat new to scripting.

The quickest and easiest way is probably to use the .Touched event. It can be annoying to work with because it fires a lot of times so you’ll have to debounce it.

That said, I’d only recommend finding alternative methods if you’re truly confused by the other explanations in this thread. As others have mentioned, a “Humanoid” is not a physical object in the 3D world. To have the sword ignore everything that’s not a living being, simply check to see if the object it collided with is part of a model which contains a humanoid.

I’m not sure what I did but I did something and it worked, thank you for your help