Raycasting only works very close to the hitpart

My raycasting only works when its super close to the hitpart

[demo - YouTube](my video)

I have been looking for so long and I cant find anyone else who has the same problem

my script:

local tool = script.Parent
local remote = tool:WaitForChild("OnShot")

local debris = game:GetService("Debris")

local TweenService = game:GetService("TweenService")

local function CreateBeam(origin, direction)
	local midpoint = origin + direction/2 

	local part = Instance.new("Part")
	part.Parent = workspace
	part.Anchored = true 
	part.CanCollide = false 

	part.Material = Enum.Material.Neon
	part.BrickColor = BrickColor.new("Really red")

	part.CFrame = CFrame.new(midpoint, origin)
	part.Size = Vector3.new(.5, .5, direction.magnitude)

	debris:AddItem(part, 1)
end

remote.OnServerEvent:Connect(function(player, mousePos, originPos)
	
	local direction = (mousePos - originPos).Unit * 300000000000

	local result = workspace:Raycast(originPos, direction)

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

		if humanoid and humanoid ~= player.Character.Humanoid then 
			humanoid.Health -= 5
		end

	end

	CreateBeam(originPos, direction)
end)

Why do you multiply it by so much? Has it always been 300000000000? Maybe the huge integer is the issue.

it didn’t work with 3000, nothing works

Hmm, try adding this raycast visualizer to your code:

-- for more info on these, check out:
-- https://developer.roblox.com/en-us/api-reference/function/WorldRoot/Raycast
-- https://developer.roblox.com/en-us/api-reference/datatype/RaycastParams
local result = workspace:Raycast(origin, direction, params)

if result then
    local distance = (origin - result.Position).Magnitude
    local p = Instance.new("Part")
    p.Anchored = true
    p.CanCollide = false
    p.Size = Vector3.new(0.1, 0.1, distance)
    p.CFrame = CFrame.lookAt(origin, position)*CFrame.new(0, 0, -distance/2)
end

I’m not super good with raycasts but using this visualizer you should be able to see what’s going on, maybe this will provide clarity to your issue.

1 Like

that still doesnt work, it just shows it going slightly ahead of the gun

There is most likely an issue with the raycast running into an unfiltered-out object in between the origin and destination positions, which could be objects directly near the origin. Use the RaycastParams to apply a table that contains the player’s character (if the origin is a tool that the player is holding) and anything else that you need the raycast to ignore.

good pont I dont think I have params

i’m having the same issue. I’ve already put raycast parameters to filter the character but it’s still not working. Did putting parameters fix it for you?

nope, it did not lol (sorry character limit) it didn’;t work

make integer of the multiplication smaller due to if roblox goes to too big numbers it goes negative

i got my script working on accident and i really can’t find the reason. Here’s the code i used though:

local ray1 = char.HumanoidRootPart.Position
local ray2 = char.HumanoidRootPart.CFrame.LookVector * 180
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {char:GetDescendants()}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local rayResult = workspace:Raycast(ray1, ray2, raycastParams)