Raycast returning nil

Currently working on a turret and the shooting function is returning nil.

So far I haven’t really tried anything besides reading more on raycasting which didn’t necessarily help.

local function shoot(Head, Distance)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = {FireHole}
	local origin = FireHole.Position
	local direction = (FireHole.Position - origin).Unit * 100
	local raycastResult = workspace:Raycast(origin, direction, raycastParams)
	print(raycastResult)

That’s the part of the function which prints nil. I don’t know the cause towards this. Any assistance is appreciated.

RaycastResult is nil when it doesn’t hit any BasePart. You can read more here: WorldRoot:Raycast.

Confused because the function will only run when a player is < 15 studs from the turret. Am I missing something in my script or?

Did you mean to do?

local direction = (Head.Position - origin).Unit * 100

Oh wow that actually ended up fixing it, cheers.

1 Like

Please mark that as your solution if it helped you!

Glad it helped you :slight_smile: