Raycast returns nil?

Honestly, i’m confused. I’m certain i did everything right. Why does the raycast keep returning nil even when i hit it on a valid instance?

Some things to note:
-Barrel is an attachment under Handle
-Blacklist does not work. I tried. I already knew that because of the name being crossed off, but i tried it anyway.

Does someone know what i’m doing wrong here?

local Remote = script.Parent:WaitForChild("ShareInfo")

local Mouse = game.Players.LocalPlayer:GetMouse()

local function PerformRaycast(User)
	local RayOrigin = script.Parent.Handle.Barrel.Position
	local RayDirection = Mouse.Hit.Position

	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
	RaycastParameters.FilterType = Enum.RaycastFilterType.Exclude
	RaycastParameters.IgnoreWater = true

	local RaycastResult = workspace:Raycast(RayOrigin, RayDirection, RaycastParameters)
	if RaycastResult then
		Remote:FireServer(RaycastResult, RayDirection, RayOrigin)
	end
end

Remote.OnClientEvent:Connect(PerformRaycast)

What do you mean by valid instance? Were you able to print raycast, but it is returning nil, or are you certain that raycast is hitting something?

I am certain. I click my mouse on a part… raycast returns nil… i’m legitimately confused
Here’s the server side of the code. It’s not the buggy part, but if you just wanted to take a look then here you go(some variables have been defined earlier on the script, which i cut off)

local function DoStuff(Player, RaycastResult, RayDirection, RayOrigin)
	if RaycastResult == nil then
		return
	end
	local origin = Handle.Barrel.Position
	local direction = RayDirection
	local result = workspace:Raycast(origin, direction)
	
	local testpart = Instance.new("Part")
	testpart.Position = RaycastResult
	testpart.Parent = workspace
	testpart.Size = Vector3.new(1,1,1)
	testpart.Anchored = true

	local intersection = result and result.Position or origin + direction
	local distance = (origin - intersection).Magnitude

	if result then
		local part = result.Instance
		local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")

		if humanoid then
			humanoid:TakeDamage(math.random(8,16))
		end
	end
	task.wait(0.1)
	testpart:Destroy()
end

Remote.OnServerEvent:Connect(DoStuff)

I also put a test part. It doesn’t appear, just returns an error(which got patched by the if then return end)

Oh, if I understand this right, you are making a ray between some parts to where you click. You need to extend the ray past your mouse to ensure that the ray will hit something.

So, i just multiply my mouse’s raycast by three? Would that work?

I think you multiply the vector by some stud, I’m not too keen with modifying a raycast


Okay, the RaycastResults appear now, but the server script doesn’t work… hmm. Eh, i probably just mistyped that if then return end part.

But it’s solved now, so… thanks.

1 Like

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