Raycast isnt working on a gun

the part gunshot thingie raycast does not show

    script.Parent.Fire.OnServerEvent:Connect(function(player,mousePos)	
    local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local raycastResult = workspace:Raycast(script.Parent.Handle.Position,(mousePos - script.Parent.Handle.Position)*300, raycastParams) -- will go 300 studs

if raycastResult then
	local hitPart = raycastResult.Instance
	local model = hitPart:FindFirstAncestorOfClass("Model")

	if model then
		if model:FindFirstChild("Humanoid") then
			model.Humanoid.Health -= 20 -- deal 20 damage
		end
	end
end
end)

and this script is in a modulescript

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

  mouse.Button1Down:Connect(function()

  script.Parent.Fire:FireServer(mouse.Hit.p)

  end)

image

Use the Tool.Activated Event instead for the LocalScript:

local Plr = game.Players.LocalPlayer
local Mouse = Plr:GetMouse()
local Tool = script.Parent
local Event = Tool:WaitForChild("Fire")

Tool.Activated:Connect(function()
    Event:FireServer(Mouse.Hit.Position)
end)

I’d check for a print on your raycastResult variable, to see if it returns back nil or a valid table of properties:

local Tool = script.Parent
local Event = Tool:WaitForChild("Fire")

Event.OnServerEvent:Connect(function(player,mousePos)	
    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {player.Character}
    raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

    local raycastResult = workspace:Raycast(Tool.Handle.Position, (mousePos - Tool.Handle.Position).Unit * 300, raycastParams) -- will go 300 studs

    print(raycastResult)
    if raycastResult then
    	local hitPart = raycastResult.Instance
	    local model = hitPart:FindFirstAncestorOfClass("Model")

    	if model then
	    	if model:FindFirstChild("Humanoid") then
		    	model.Humanoid.Health -= 20 -- deal 20 damage
	    	end
    	end
    end
end)
1 Like