Raycast result prints nil

gun gun gun etc…

anyway, someone told me i had to use raycasting instead.
so here is a server script with target being the mouse.hit.Position

Connection.OnServerEvent:Connect(function(plr,Target)
	print("Fired!!!")
	local Direction = Tool.Handle.Position - Target
	
	local RayCastParams = RaycastParams.new()
	RayCastParams.FilterDescendantsInstances = {
		plr.Character
	}
	RayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	CreateProjectile(Direction,Target)
	
	local Results = workspace:Raycast(Tool.Handle.Position,Direction * Range,RayCastParams)
	print(Results)
	if Results.Instance then
		local Character = Results.Instance.Parent
		local PossibleHumanoid = Character:FindFirstChildWhichIsA("Humanoid")
		if PossibleHumanoid then
			PossibleHumanoid:TakeDamage(175)
		end
	end
end)

now here’s my problem:
raycasting returns nil, and i don’t know why.
it errors at the if statement because there’s no result.

(btw createprojectile is just a function above for cosmetic stuff like it creates a ball and tweens it the deletes it after a few seconds)
thanks in advance!

It seems your direction vector is calculated incorrectly. You should subtract Tool.Handle.Position from Target to get the vector pointing towards Target. Right now you’re getting a vector pointing in the opposite direction.

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