Hello everyone, in my game I want rays to go through a set amount of parts. Problem being, the trajectory of the ray changes as it goes through another part.
Here's what I have so far, as a snippet;
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've searched around for a good half hour and surprisingly, nobody else has had this issue.
