Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Raycasting that ignores Hats?

I am working on a gun, and everything is working as it should, but I have one problem: Hats keep getting in the way of headshots etc. Any way to have a ray ignore hats? Here is the raycast section of my main script...

        local ray = Ray.new(tool.Barrel.CFrame.p, (mouse.Hit.p - tool.Barrel.CFrame.p).unit*300)
        local hit, position = game.Workspace:FindPartOnRay(ray, user)
	script.Parent.Handle.Fire:Play()
        local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
			if not hit.Parent:FindFirstChild("ForceField") then
				if hit.Name == "Torso" then
					humanoid:TakeDamage(70)
				elseif hit.Name == "Head" then
					humanoid:TakeDamage(87)
					humanoid.WalkSpeed = 14
				else
					humanoid:TakeDamage(45)
				end
			end
			humanoid.Died:connect(function()
				if enabled == true then
					giveKills(hit.Parent)
					enabled = false
				end
			end)
        end

1 answer

✓ Accepted answer

Well, You can't really make the ray ignore the hat, But you can edit your script too see if the hit.Parent:IsA("Hat")

if Hit.Parent:IsA("Hat") then
--Do the regular damaging part of the script
else
-- You would have to set everything to hit.Parent.Parent.Humanoid, Which would allow it to damage the humanoid.
end

4 comments

0

Thanks, it worked!

0

No problem.

0

if it IS a hat, continue? and if NOT, do different stuff?

0

And on top of that, there is no reason to use :isA() if you're just checking the class, and i believe it may be slower.

This discussion is preserved as it was. New answers, comments, and votes are closed.