Hat Accessories blocking Mouse.Target

When I shoot the head, the top half of the head is blocked because of the accessories

I tried to get around this using this but no luck (changed it to look a bit nicer to read)

           if target.Parent:IsA("Hat") then
				hathum.Health -= 30
				local sound = script.Headshot:Clone()
				sound.Parent = target
				sound:Destroy()
			elseif target.Parent:IsA("Accessory") and target.Parent.AccessoryType == (Enum.AccessoryType.Face or Enum.AccessoryType.Hair or Enum.AccessoryType.Hat or Enum.AccessoryType.Eyebrow or Enum.AccessoryType.Eyelash) then
				hathum.Health -= 30
				local sound = script.Headshot:Clone()
				sound.Parent = target
				sound:Destroy()
			else
				hathum.Health -= 10
				local sound = script.Hit:Clone()
				sound.Parent = target
				sound:Destroy()
			end
4 Likes

Try using raycast instead of Mouse.Target.

local GunParams = RaycastParams.new()
GunParams.FilterType = Enum.RaycastFilterType.Exclude
GunParams.IgnoreWater = true
GunParams.FilterDescendantsInstances = {playerCharacterHere}

local function Raycast(Origin, Direction)
	local Result = workspace:Raycast(Origin, Direction, GunParams)
	if Result then
		local HitPart = Result.Instance
		local Model = HitPart:FindFirstAncestorOfClass("Model")
		local Humanoid = Model and Model:FindFirstChild("Humanoid")

		if Humanoid and HitPart.Parent ~= Model then
			local Ignored = GunParams.FilterDescendantsInstances
			table.insert(Ignored, HitPart)
			GunParams.FilterDescendantsInstances = Ignored

			return Raycast(Origin, Direction)
		end

		return Result
	end
end

local Result = Raycast(firePoint.WorldPosition, (mouse.Hit.Position - firePoint.WorldPosition).Unit * bulletMaxDistance)
if Result and Result.Instance.Parent:FindFirstChild("Humanoid") then
	-- damage humanoid
end
4 Likes

This wouldn’t work because the script isn’t a local script and the filter would end up resetting over and over if it hit 2 accessories due to also having to put the player in the function which would also end up storing multiple players giving those players god mode.

1 Like

If it hit 2 accessories it would add them to the excluded list and redo the ray, which means it would hit the head.

About the player storing thing, you could try this:

local GunParams = RaycastParams.new()
GunParams.FilterType = Enum.RaycastFilterType.Exclude
GunParams.IgnoreWater = true
GunParams.FilterDescendantsInstances = {}

Tool.Equipped:Connect(function()
	local Ignored = GunParams.FilterDescendantsInstances
	table.insert(Ignored, Tool.Parent)
	GunParams.FilterDescendantsInstances = Ignored
end)

local Result = Raycast(firePoint.WorldPosition, (mouse.Hit.Position - firePoint.WorldPosition).Unit * bulletMaxDistance)

if Result then
	local HitPart = Result.Instance
	local Humanoid = HitPart.Parent:FindFirstChild("Humanoid")

	Humanoid:TakeDamage(GunDamage)
end

I’d recommend you to raycast on the client and send the info to the server (it is exploitable, but you could try adding sanity checks).

2 Likes

I tweaking your code a bit to fit into my script but now it wont do any damage unless I’m right up in the player’s face

Current Whole Script

local raypara = RaycastParams.new()
raypara.FilterType = Enum.RaycastFilterType.Exclude
raypara.IgnoreWater = true
raypara.FilterDescendantsInstances = {}
local function Raycast(Origin, Direction)
	local Result = workspace:Raycast(Origin, Direction, raypara)
	if Result then
		local HitPart = Result.Instance
		local Model = HitPart:FindFirstAncestorOfClass("Model")
		local Humanoid = Model and Model:FindFirstChild("Humanoid")
		print(Result.Instance.Name)
		if Humanoid and HitPart.Parent ~= Model then
			local Ignored = raypara.FilterDescendantsInstances
			table.insert(Ignored, HitPart)
			raypara.FilterDescendantsInstances = Ignored

			return Raycast(Origin, Direction)
		end

		return Result
	end
end
shot.OnServerEvent:Connect(function(player,tip,hitpos,tippos)
	tip.SpotLight.Enabled = true
	local blood = nil
	raypara.FilterDescendantsInstances = {player.Character}
	local Result = Raycast(tippos,hitpos)
	if Result then
		local HitPart = Result.Instance
		local Humanoid = HitPart.Parent:FindFirstChild("Humanoid")

		if HitPart.Name == "Head" then
			Humanoid:TakeDamage(30)
			local sound = script.Headshot:Clone()
			sound.Parent = HitPart
			sound:Destroy()
		else
			Humanoid:TakeDamage(10)
			local sound = script.Hit:Clone()
			sound.Parent = HitPart
			sound:Destroy()
		end
		blood = script.Blood:Clone()
		blood.Parent = HitPart
	end
	task.wait(0.1)
	tip.SpotLight.Enabled = false
	if blood ~= nil then
		task.wait(0.25)
		blood.Rate = 0
		task.wait(1)
		blood:Destroy()
	end
end)
1 Like

Can you tell me what the hitpos and tippos variables are??

1 Like

hitpos is Mouse.Hit.Position
tippos I switched to the player’s camera position since the game will be first person only

Alright, try changing

Raycast(tippos,hitpos)

to

Raycast(tippos, (hitpos - tippos).Unit * 100) -- 100 is the max bullet distance, you can change it if you want

That seems to work but now it errors because it cant find a humanoid to damage which causes a visual bug

I was able to fix by adding a not before the humanoid (Nevermind, that just blocks the hats again)

       if Humanoid and HitPart.Parent ~= Model then
			local Ignored = raypara.FilterDescendantsInstances
			table.insert(Ignored, HitPart)
			raypara.FilterDescendantsInstances = Ignored

			return Raycast(Origin, Direction)
		end

Thanks for helping me!

1 Like

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