Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

Raycasting that ignores Hats?

Asked by
hudzell 238 Moderation Voter
10 years ago

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

Log in to vote
3
Answered by 10 years ago

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
0
Thanks, it worked! hudzell 238 — 10y
0
No problem. xImmortalChaos 565 — 10y
0
if it IS a hat, continue? and if NOT, do different stuff? iaz3 190 — 10y
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. iaz3 190 — 10y
Ad

Answer this question