Raycast not going where it should

hi, i’m making my first gun but when i shoot the raycast goes backwards to me when i’m in first person and the raycast somethimes hit the gun owner

my code here:


local function shootRaycast(plr, fromPosition, toPosition)

	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {plr.Character}
	params.FilterType = Enum.RaycastFilterType.Blacklist

	local ttrail = Ray.new(fromPosition, (toPosition - fromPosition).Unit * 500, params)
	local hitpart, hitpos, normal = game.Workspace:FindPartOnRayWithIgnoreList(ttrail, ignorelist, false, true)


	if hitpart then

		if hitpart.Name == "Handle" and hitpart.Parent:IsA("Accessory") or hitpart.Parent.Name == "Handle" then
			table.insert(ignorelist, hitpart)
			return shootRaycast(plr, fromPosition, toPosition)
		else
			return hitpart
		end
	end
end


and i put all the variables when i call the function

shouldnt this line just be

local result = workspace:Raycast(origin, direction, rayparams)

Also, by the looks of it, you create a trail to display the bullet, try adding that to the ignore list too.

Heres a little snippet of code from my perfectly working (but exploitable) gun system

		local NewRay = RaycastParams.new()
		local RayOrigin = Camera.CFrame.Position
		local RayDiretion = Camera.CFrame.LookVector*500
		-- I could have done this line 1000x better, but i am no longer working on this system
		local BulletOffset = CFrame.new(RayOrigin, (RayDiretion*500))*CFrame.Angles(math.rad(math.random((-Current_Spread/100)*SpreadMultiplier, (Current_Spread/100)*SpreadMultiplier)), math.rad(math.random((-Current_Spread/100)*SpreadMultiplier, (Current_Spread/100)*SpreadMultiplier)), math.rad(math.random((-Current_Spread/100)*SpreadMultiplier, (Current_Spread/100)*SpreadMultiplier)), 0)
		local RaycastBlacklist = GetRaycastBlacklist()
		NewRay.FilterDescendantsInstances = RaycastBlacklist
		NewRay.FilterType = Enum.RaycastFilterType.Blacklist

		local Result = workspace:Raycast(RayOrigin, BulletOffset.LookVector*10000, NewRay)
		
		if Result then
			-- stuff
		end

Edit: Forgot to add this, but the GetRaycastBlacklist function is this:

local script function:

	local RaycastBlacklist = RaycastModule:GetRaycastBlacklist()

	table.insert(RaycastBlacklist, Character)
	table.insert(RaycastBlacklist, ViewModel)
	table.insert(RaycastBlacklist, ViewmodelRotationOffsetPart)

	return RaycastBlacklist

module script:

local module = {}

module.GetRaycastBlacklist = function()
	local RaycastBlacklist = {}
	for i ,v in pairs(workspace.Checks:GetChildren()) do
		table.insert(RaycastBlacklist, v)
	end
	for i ,v in pairs(workspace.Bullet_Holes:GetChildren()) do
		table.insert(RaycastBlacklist, v)
	end
	for i ,v in pairs(workspace.Bullet_Trails:GetChildren()) do
		table.insert(RaycastBlacklist, v)
	end
	for i, v in pairs(workspace:GetChildren()) do
		if v:IsA('Model') and v:FindFirstChildOfClass('Humanoid') then
			for i, m in pairs(v:GetChildren()) do
				if m:IsA('Accessory') or m:IsA('Hat') then
					table.insert(RaycastBlacklist, m)
				elseif m.Name == 'Right Arm' and m:FindFirstChildOfClass('Model') then
					table.insert(RaycastBlacklist, m:FindFirstChildOfClass('Model'))
				elseif m.Name == 'Right Arm' and m:FindFirstChildOfClass('Model') and m:FindFirstChildOfClass('Model'):FindFirstChildOfClass('Model') then
					table.insert(RaycastBlacklist, m:FindFirstChildOfClass('Model'):FindFirstChildOfClass('Model'))
				end
			end
		end
	end
	for i, v in pairs(workspace.Map.Spawns:GetChildren()) do
		table.insert(RaycastBlacklist, v)
	end
	for i, v in pairs(workspace.Map:GetChildren()) do
		if v:IsA('Part') and v.CanCollide == false or v:IsA('MeshPart') and v.CanCollide == false or v:IsA('UnionOperation') and v.CanCollide == false then
			table.insert(RaycastBlacklist, v)
		elseif v.Name == 'Vault' then
			table.insert(RaycastBlacklist, v)
		elseif v.Name == 'Barrier' then
			table.insert(RaycastBlacklist, v)
		elseif v:IsA('Model') and v.Name ~= 'O_O_B_Detail' then
			for i, c in pairs(v:GetChildren()) do
				if v:IsA('Part') and v.CanCollide == false or v:IsA('MeshPart') and v.CanCollide == false or v:IsA('UnionOperation') and v.CanCollide == false then
					table.insert(RaycastBlacklist, v)
				end
			end
		end
	end
	for i, v in pairs(workspace.Medbags:GetChildren()) do
		table.insert(RaycastBlacklist, v)
	end
	for i ,v in pairs(workspace.Ragdolls:GetChildren()) do
		table.insert(RaycastBlacklist, v)
	end
	return RaycastBlacklist
end

return module
1 Like