#Need help raycasting returning nil

1 messages · Page 1 of 1 (latest)

supple tusk
#
local DataManager = require(game.ServerScriptService.Data.DataManager)
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Framework = ReplicatedStorage:WaitForChild("Framework")
local ShootEvent = Framework.Modules.Events:WaitForChild("Shoot")

local MAX_RAYCAST_DISTANCE = 200
local HEADSHOT_DAMAGE = 50
local BODYSHOT_DAMAGE = 15

local function visualizeRaycast(origin, hitPosition)
    local rayLength = (hitPosition - origin).Magnitude
    local rayPart = Instance.new("Part")
    rayPart.Anchored = true
    rayPart.CanCollide = false
    rayPart.CanQuery = false
    rayPart.Transparency = 0.5
    rayPart.Material = Enum.Material.Neon
    rayPart.BrickColor = BrickColor.new("Bright red")
    rayPart.Size = Vector3.new(0.1, 0.1, rayLength)

    -- Position part to point from origin to hitPosition, centered properly
    rayPart.CFrame = CFrame.new(origin, hitPosition) * CFrame.new(0, 0, -rayLength / 2)
    rayPart.Parent = workspace

    game:GetService("Debris"):AddItem(rayPart, 1)
end

ShootEvent.OnServerEvent:Connect(function(player, mousePosition, BarrelPosition)
    if not player.Character then 
        print("[Raycast] Player has no character.")
        return 
    end

    local direction = (mousePosition - BarrelPosition)

    if direction.Magnitude < 0.1 then
        return
    end

    if direction.Magnitude > MAX_RAYCAST_DISTANCE then
        direction = direction.Unit * MAX_RAYCAST_DISTANCE
    end


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

    local raycastResult = workspace:Raycast(BarrelPosition, direction, raycastParams)
    
    print(raycastResult)
    
    if raycastResult then
        visualizeRaycast(BarrelPosition, raycastResult.Position)
    else
        visualizeRaycast(BarrelPosition, BarrelPosition + direction)
    end

    -- Continue with your existing hit logic...
    if raycastResult then
        local hitPart = raycastResult.Instance

        local enemyCharacter = hitPart and hitPart:FindFirstAncestorWhichIsA("Model")
        local enemyHumanoid = enemyCharacter and enemyCharacter:FindFirstChild("Humanoid")

        if enemyHumanoid and enemyHumanoid.Health > 0 then
            local damage = BODYSHOT_DAMAGE
            if hitPart.Name == "Head" then
                damage = HEADSHOT_DAMAGE
                print("[Raycast] Headshot!")
            else
                print("[Raycast] Body shot!")
            end

            enemyHumanoid:TakeDamage(damage)

            local victimPlayer = Players:GetPlayerFromCharacter(enemyCharacter)
            if victimPlayer and enemyHumanoid.Health <= 0 then
                print("[Raycast] Player dead:", victimPlayer.Name)
                DataManager.AddDeaths(victimPlayer, 1)
                DataManager.AddKills(player, 1)
            end
        else
            print("[Raycast] Hit part has no valid humanoid or humanoid is dead.")
        end
    else
        print("[Raycast] No hit detected.")
    end
end)```
#

im going insanse trying to figure this out anyone have any ideas?

#

the tool and character are already filtered

manic night
#

increase the length of the ray

#

you are using mouse.hitpos which is actually on the surface of the part, it does not intersect the part

#

therefore no hit