Having issues with RaycastResult

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

oh oops change 1000 to like -1000 so its on opposite direction!

Seems to have done the trick, no idea why!

100% consistency now- but why did that work?
Also, what about when I shoot in the sky? I don’t know how to fix that, it doesn’t fire if you shoot into the sky

Uhhhh Mouse.Hit.p isnt really reliable try using ScreenToPointRay lol ez

What? Do you mean Camera:ScreenPointToRay()?

1 Like

Ray.new is not deprecated. The new WorldRoot::Raycast API does not use Ray, but there may be other APIs in the future which do, and the type still gets returned from ScreenPointToRay.

2 Likes