Having issues with RaycastResult

Hi! I’m working with Raycasts, and lots of the time my script is printing “no raycastresult??”. I’m unsure as to why, as my mouse is on a part, and my character is ignored. There seems to be no consistency as to when RaycastResult fails.

When RaycastResult is returned, the function works perfectly. MouseHit is just Mouse.Hit.p.

Here's my code:
function Shoot(MouseHit)
	local Params = RaycastParams.new()
	local FromPosition = BodyAttach.Position
	print(MouseHit)
	local ToPosition = MouseHit
	local Direction = ToPosition - FromPosition
	Params.FilterDescendantsInstances = {Character}
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	local RaycastResult = workspace:Raycast(FromPosition, Direction, Params)
	if RaycastResult then
		local StartAttachment, EndAttachment = Instance.new('Attachment', workspace.Beams), Instance.new('Attachment', workspace.Beams)
		StartAttachment.WorldPosition = BodyAttach.Position
		EndAttachment.WorldPosition = RaycastResult.Position
		local Beam = script.BowBeam:Clone()
		Beam.Attachment0 = StartAttachment
		Beam.Attachment1 = EndAttachment
		Beam.Parent = workspace.Beams
		Beam.Enabled = true
	else
		print('no raycastresult??')
	end
end
1 Like

Well it may be because your computing the direction for the ray incorrectly, instead do this:

function Shoot(MouseHit)
	local Params = RaycastParams.new()
	local FromPosition = BodyAttach.Position
	print(MouseHit)
	local ToPosition = MouseHit
	local Offset = ToPosition - FromPosition
	Params.FilterDescendantsInstances = {Character}
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	local RaycastResult = workspace:Raycast(FromPosition, Offset.Magnitude * Offset.Unit, Params)
	if RaycastResult then
		local StartAttachment, EndAttachment = Instance.new('Attachment', workspace.Beams), Instance.new('Attachment', workspace.Beams)
		StartAttachment.WorldPosition = BodyAttach.Position
		EndAttachment.WorldPosition = RaycastResult.Position
		local Beam = script.BowBeam:Clone()
		Beam.Attachment0 = StartAttachment
		Beam.Attachment1 = EndAttachment
		Beam.Parent = workspace.Beams
		Beam.Enabled = true
	else
		print('no raycastresult??')
	end
end

Didn’t help at all.
image

Ensure the BodyAttach is positioned properly because the rest of the code is fine.

If the raycast doesn’t hit anything it will print no raycast result

Here's a gif.

My code:
function Shoot(MouseHit)
	local Params = RaycastParams.new()
	local FromPosition = BodyAttach.Position
	local ToPosition = MouseHit
	local Offset = ToPosition - FromPosition
	Params.FilterDescendantsInstances = {Character}
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	local RaycastResult = workspace:Raycast(FromPosition, Offset.Magnitude * Offset.Unit, Params)
	if RaycastResult then
		local StartAttachment, EndAttachment = Instance.new('Attachment', workspace.Beams), Instance.new('Attachment', workspace.Beams)
		StartAttachment.WorldPosition = BodyAttach.Position
		EndAttachment.WorldPosition = RaycastResult.Position
		StartAttachment.Visible = true
		EndAttachment.Visible = true
		local Beam = script.BowBeam:Clone()
		Beam.Attachment0 = StartAttachment
		Beam.Attachment1 = EndAttachment
		Beam.Parent = workspace.Beams
		Beam.Enabled = true
		print('found raycastresult')
		wait(0.2)
		--BodyAttach.BowBeam.Enabled = false
	else
		print('no raycastresult??')
	end
end

Shouldn’t the raycast be hitting something though? Refer to the gif/code above

Can you send the code where your calling function?

Of course.

Server
Event.OnServerEvent:Connect(function(PlayerSent, Action, ...)
	if PlayerSent == Player then
		if Action == 'PlaySound' and Lib.RemoteFloodCheck(Player, 'Write', Event, Action, 20) then
			PlaySound(...)
		elseif Action == 'Fire' and Lib.RemoteFloodCheck(Player, 'Write', Event, Action, 10) then
			Shoot(...)
		end
	end
end)
Client
Tool.Equipped:Connect(function(Mouse)
	Equipped = true
	Lib.ToolAttach('Attach', BodyAttach, Character.HumanoidRootPart)
	Animations['Hold']:Play()
	table.insert(Connections, Mouse.Button1Down:Connect(function()
		MouseUp = false
		Animations['Hold']:Stop()
		Animations['Reload']:Play()
		Event:FireServer('PlaySound', 'Reload', true)
		Animations['Reload'].Stopped:Wait()
		if Equipped and not MouseUp then
			Animations['Draw']:Play()
			Event:FireServer('PlaySound', 'Draw', true)
			Animations['Draw'].Stopped:Wait()
			if Equipped and not MouseUp then
				Animations['Aim']:Play()
				ReadyToFire = true
			end
		end
	end))
	table.insert(Connections, Mouse.Button1Up:Connect(function()
		MouseUp = true
		Animations['Reload']:Stop()
		Animations['Draw']:Stop()
		Animations['Aim']:Stop()
		Event:FireServer('PlaySound', 'Reload', false)
		Event:FireServer('PlaySound', 'Draw', false)
		if ReadyToFire then
			ReadyToFire = false
			Tool.Arrow.Transparency = 1
			Animations['Release']:Play()
			Event:FireServer('PlaySound', 'Shoot', true)
			Animations['Release'].Stopped:Wait()
			if Equipped then
				Tool.Arrow.Transparency = 0
				Event:FireServer('Fire', Mouse.Hit.p) -- <-- Here's what you're looking for
			end
		end
		Animations['Hold']:Play()
	end))
end)

Maybe try doing this

local character = game.Players.LocalPlayer.Character
function raycast(origin,endpos,distance)
local ray = Ray.new(origin,(origin-endpos).Unit*Distance)
local hit,pos,norm = workspace:FindPartOnRay(ray,character)
if hit then
print(hit.Name)
else
print('no hit')
end
end

Ray.new() is depreciated. I’m trying not to use depreciated functions in my work.

Wait what the hell so FPS games are like dead rn? lol?

No, Ray.new() still works. It’s just depreciated.

Hm alright ill try using Raycast function then well

local Head = game.Players.LocalPlayer.Head
local Params = RaycastParams.new()
workspace:Raycast(Head.Position,(Head.Position - MouseHit).Unit*1000,Params)

if it doesnt work idk im new to that function aswell LOL

I don’t think that would work at all lol

lol maybe try to use it… u know… ewz

I mean, sure.


Remove the .Position on the fromPosition from 2nd arg?

Oops, my bad.

So here's that result:

Better, but still not 100% consistency. Also, it’s going in the completely wrong direction.
image