Raycast Returning No Instance

I’m trying to make a quick gun system, but I can’t seem to always get the raycast correctly toward the players cursor.

Here is the code.

local r = workspace:Raycast(FirePos.Position,(MousePos-FirePos.Position),params)
		if r then
			print(r.Instance)
		else 
			warn("No Raycast Instance")
		end

image

I clicked on the baseplate that whole time, and still, only half the time did it have an instance.

Video:

All animations are handled on the server. The only thing on the client is the mouse detection and ammo.

I have a script in starter character that lets the arm follow the mouse, which is also serversided

StarterCharacterScripts ArmsFollowMouse Code

image
Client:

local CFnew, CFang = CFrame.new, CFrame.Angles
local asin = math.asin
local mouse = game.Players.LocalPlayer:GetMouse()

script.Parent:WaitForChild("Torso")

local c = coroutine.create(function()
	while wait(.1) do
		if game.Players.LocalPlayer.Name == script.Parent.Name then
			local T = "Torso"
			if script.Parent:WaitForChild(T) then
				local T1 = "Left Shoulder"
				if script.Parent:WaitForChild(T):WaitForChild(T1) then 
					local R = script.Parent:WaitForChild(T):WaitForChild(T1).C0.Rotation
					local T = script.Parent:WaitForChild(T):WaitForChild(T1).C0
					local cframe = CFnew(-1, .5, 0) * CFang(-asin((mouse.Origin.p - mouse.Hit.p).Unit.y), -1.55, R.Z)
					script.RemoteEvent:FireServer(cframe,script.Parent:WaitForChild("Torso"):FindFirstChild("Left Shoulder"))
				end

				local T1 = "Right Shoulder"
				if script.Parent:WaitForChild(T):WaitForChild(T1) then 
					local R = script.Parent:WaitForChild(T):WaitForChild(T1).C0.Rotation
					local T = script.Parent:WaitForChild(T):WaitForChild(T1).C0
					--			script:FindFirstChild("Right Shoulder").C0.
					local cframe = CFnew(1, .5, 0) * CFang(-asin((mouse.Origin.p - mouse.Hit.p).Unit.y), 1.55, R.Z)
					script.RemoteEvent:FireServer(cframe,script.Parent:WaitForChild("Torso"):FindFirstChild("Right Shoulder"))
				end
			end
		end
	end
end)

coroutine.resume(c)

Server:

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player,cfrrr,weld)
	local t = game.TweenService:Create(weld,TweenInfo.new(.3),{C0 = cfrrr})
	t:Play()
	t.Completed:Connect(function()
		t:Remove()
	end)
end)

All Parts in the gun have cancollide,canquery, and cantouch false.

Try this:
local r = workspace:Raycast(FirePos.Position,(MousePos-FirePos.Position)*1.2,params)

1 Like

What’s the code you use to get the Fireposition?

1 Like

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