Help with RayCastResult always resulting in nil

I’m working on learning how to code projectiles using RayCast however when I try this code I wrote, the raycast result is always resulting in nil no matter what I’m hovering over with my mouse.

(objHit isn’t working because the raycast result is nil)

local plr = game.Players.LocalPlayer
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local humRP = char:WaitForChild("HumanoidRootPart")

local RS = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local UIS = game:GetService("UserInputService")


local debounce = false
local CD = .1

local tpRemote = RS.Remotes.Teleport

local mouse = plr:GetMouse()

local KEY = Enum.KeyCode.V

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {char}

local rayOrigin = humRP.Position
local rayDirection = mouse.Hit.Position

local raycastResult = game.Workspace:Raycast(rayOrigin, rayDirection, raycastParams)

local function throwKunai()
	local objHit = raycastResult.Instance
	local kunai = RS.FX.RaijinKunai:Clone()
	
	print(objHit)
end

UIS.InputBegan:Connect(function(inp, gpe)
	if gpe then return end
	if inp.KeyCode == KEY and not debounce then
		debounce = true
		throwKunai()
		task.wait(CD)
		debounce = false
	end
end)
1 Like

try putting these lines:

inside the throwKunai function

1 Like

That worked, sorry I’m an idiot :sob:

its all good lol ive made the same mistake before

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.