In the while loop, when it casts the ray, it just hits the viewmodel which annoys me a lot.
local ig = {game.Workspace.CurrentCamera,Character} --the list with the ignores in it
for i = 1, gunfolder.Bullets.Value do
if gunfolder.Bullets.Value == 1 then
print(i)
local dir
dir = CFrame.new(p1,p2)*CFrame.Angles(math.rad(math.random(gunfolder.Spread.Value/10,gunfolder.Spread.Value/10)),math.rad(math.random(gunfolder.Spread.Value/10,gunfolder.Spread.Value/10)),0)
local size = 1
local increment = 1 -- how big your backwards ray will increase every iteration
local penetrationDepth = 5 --how far your ray is allowed to go through the wall
local origin = p1 -- the origin of your original ray
local hitpos = p2
local hit, pos
while size < penetrationDepth do --courtesy of rek_kie https://devforum.roblox.com/t/bullet-wallbanging-is-not-functioning-properly/1031719
local rey = Ray.new(hitpos, p1,dir.LookVector* size)
local hit, position,normal = workspace:FindPartOnRayWithIgnoreList(rey, ig, false, true)
print(hit)
if not hit then
size += increment
else
if hit.Parent:IsA("Accessory") then
table.insert(ig,hit) -- adds to the ignore list
return Raycast(p1,p2,dist) -- do the raycast again
end
break
end
local ray = Ray.new(p1,dir.LookVector*dist)
local clone = game.ReplicatedStorage.ray:Clone()
clone.Parent = game.Workspace
clone.p1.WorldPosition = origin
clone.p2.WorldPosition = hitpos
game.Debris:AddItem(clone,2)
local part, position,normal = workspace:FindPartOnRayWithIgnoreList(ray, ig, false, true)
if part then
if part.Parent:IsA("Accessory") then
table.insert(ig,part) -- adds to the ignore list
return Raycast(p1,p2,dist) -- do the raycast again
else
print(part.Name,part.Parent.Name)
game.ReplicatedStorage.cast:FireServer(part,gunfolder)
if config.KillAndHitSounds then
if part.Parent then
if part.Parent:FindFirstChild("Humanoid") then
game.SoundService:PlayLocalSound(game.ReplicatedStorage.Sounds.Hit)
end
end
end
local newcastpos = p2
local ray = Ray.new(newcastpos,dir.LookVector*dist)
local part, position,normal = workspace:FindPartOnRayWithIgnoreList(ray, ig, false, true)
if part then
local clone = game.ReplicatedStorage.ray:Clone()
clone.Parent = game.Workspace
clone.p1.WorldPosition = origin
clone.p2.WorldPosition = position
game.Debris:AddItem(clone,2)
if part.Parent:IsA("Accessory") then
table.insert(ig,part) -- adds to the ignore list
return Raycast(p1,p2,dist) -- do the raycast again
else
print(part.Name,part.Parent.Name)
newcastpos = part.Position
game.ReplicatedStorage.cast:FireServer(part,gunfolder,true)
end
end
return part,position,normal
end
end
end
end
end
end