luarobloxraycastingroblox-studio

How can I detect if the raycast hits the player in roblox studio?


local PathfindingService = game:GetService("PathfindingService")


local Humanoid = script.Parent.Humanoid
local Root = script.Parent.HumanoidRootPart
local goal = script.Parent.Goal -- Change the name of the goal to your Part that you want it to reach.
local prompt = game.Workspace.KickDoor.Door.KickPromptAttachment.ProximityPrompt


prompt.Triggered:Connect(function(plr)
    local playerhumanoid = plr.Character:WaitForChild("Head")
    local path = PathfindingService:CreatePath()
    path:ComputeAsync(Root.Position, goal.Position)
    local waypoints = path:GetWaypoints()
    for i, waypoint in ipairs(waypoints) do
        Humanoid:MoveTo(waypoint.Position)
        if waypoint.Action == Enum.PathWaypointAction.Jump then
            Humanoid.Jump = true
        end
        local RunService = game:GetService("RunService")
        Humanoid.MoveToFinished:Connect(function()
            RunService.Heartbeat:Connect(function()
                Root.CFrame = CFrame.lookAt(
                    Root.Position,
                    playerhumanoid.Position * Vector3.new(1,0,1)
                        + Root.Position * Vector3.new(0,1,0))
                while true do
                    local a = script.Parent.Head
                    local b = playerhumanoid

                    local direction = b.position - a.Position

                    local raycastResult = workspace:Raycast(a.Position,direction)

                    if raycastResult and raycastResult.Instance then

                        raycastResult.Instance.Color = Color3.new(1,0,0)
                        
                    end
                    task.wait(1)
                end
            end)
        end)
    end
end)

Humanoid.Died:Connect(function()
    local ragdoll = game.ReplicatedStorage.Ragdoll
    local clonedModel = ragdoll:Clone()
    clonedModel.Parent = workspace
    clonedModel:MoveTo(Humanoid.Parent.HumanoidRootPart.Position-Vector3.new(0,-5.5,0))
    wait(.1)
    Humanoid.Parent:Destroy()
end)

How can I detect if the raycast coming from the NPC hits the player? Currently the script Detects parts between the player and the NPC, and changes the color of those detected parts. The desired functionality is that when the player is not behind a part obscuring the NPC's vision, the NPC detects the player.


Solution

  • You can check the RaycastResult's ancestor for a Humanoid to determine if your Raycast has hit a Player.

    Since every player's Character is a Model and has a Humanoid child, if your raycast hits a model with a Humanoid we can safely assume that we've hit the player's character.

    From there you can use Players:GetPlayerFromCharacter to get the Player from its Character.

    if raycastResult and raycastResult.Instance then
        raycastResult.Instance.Color = Color3.new(1, 0, 0)
    
        -- The instance hit will be a child of a character model
        -- If a humanoid is found in the model then it's likely a player's character
        local characterModel = raycastResult.Instance:FindFirstAncestorOfClass("Model")
        if characterModel then
            local humanoid = characterModel:FindFirstChild("Humanoid")
            if humanoid then
                print("Detected " .. characterModel.Name)
            end
        end
    end
    

    The above code will check if the Raycast has an ancestor that is a Model and then if that model has a Humanoid. If we do then, in this case, print that we detect the player along with the player's name.