Raycast not working consistently

I’m running this code out of a server script, using a raycast on the server script for the hit detection on a gun. Weirdly even when shooting a player standing completely still and not moving my aim it only seems to register half of the time. The raycast also seems to never register when hitting a player’s head. I’m not sure exactly what is wrong with it but the server script is below.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local pistolFire = ReplicatedStorage:WaitForChild("pistolFire")
local Players = game:GetService("Players")
local v = {}
local filter = {}

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        for _, A in pairs(Character:GetDescendants()) do
            if A:IsA("Accessory") or A:IsA("Accoutrement") then
                table.insert(filter, A)
            end
        end
    end)
end)

local function bounceRemote(player, projvalues)
    local rayOrigin = projvalues[1]
    local rayDirection = projvalues[4]
    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {player.Character, filter}
    raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
    local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
    if raycastResult then
        local hitPlayer = raycastResult.Instance.Parent:FindFirstChild("Humanoid")
        if hitPlayer then
            hitPlayer.Health = hitPlayer.Health - 10
        end
    end
    for _, other in Players:GetPlayers() do
        if other ~= player then
            v[1] = projvalues[1]
            v[2] = projvalues[2]
            v[3] = projvalues[3]
            pistolFire:FireClient(other, v)
        end
    end
end

pistolFire.OnServerEvent:Connect(bounceRemote)

Any help is appreciated!

1 Like