Hello everyone, I want the rays casted by my guns to go through a certain amount of parts, and while the ray does indeed go through the parts, the ray doesn’t follow the mouse’s direction. As seen here.
When I initially wanted the rays to go through parts, I followed this post.
I couldn’t find anyone else running into a similar issue.
Here’s a snippet of the code;
local tool = script.Parent;
local char, ammo_count, remote_event;
tool.Equipped:Connect(function()
char = tool.Parent
end)
function gun_fire(plr, firePoint, toPoint)
local char = tool.Parent --firePoint is an attachment on the gun model, toPoint is the mouse location
if plr == game.Players:GetPlayerFromCharacter(char) then
local ignore_list = char:GetDescendants()
local victims = {} --We don't want to hit the same target twice, do we?
local count = 3 --How many times the bullet can go through parts
for i = 0, count, 1 do --Loops 3 times
local ray = Ray.new(firePoint.WorldCFrame.p, (toPoint.p - firePoint.WorldCFrame.p).Unit * 500)
--Creates a ray from the firePoint to the mouse position
local part, position = workspace:FindPartOnRayWithIgnoreList(ray, ignore_list)
local bullet_hole = Instance.new("Part") do
bullet_hole.Parent = workspace.Ignore
bullet_hole.Size = Vector3.new(0.25, 0.25, .25)
bullet_hole.Anchored = true
bullet_hole.CanCollide = false
bullet_hole.Position = position
bullet_hole.Color = Color3.new(0, 0, 0)
bullet_hole.Shape = Enum.PartType.Ball
end
table.insert(ignore_list, bullet_hole)
if part then
table.insert(ignore_list, part)
local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
--Finds a humanoid even if we hit an accessory
if humanoid and not table.find(victims, humanoid.Parent) then
table.insert(victims, humanoid.Parent)
humanoid:TakeDamage(412798018924970124907142)
end
end
end
end
end
remote_event.OnServerEvent:Connect(gun_fire)
I’m still pretty new to raycasting, so my method may be inefficient. All help is appreciated!