Hello there. I am making a gun penetration so a bullet can damage multiple entities if they are all standing on the ray. But when I test it, it comes to a problem.
The script is supposed to mark the hit part on raycast, blacklist it, and continue the ray at the last hit position. Fow some reason, when the bullet hits an arm or a leg it updates the position only once and stuck at it forever. You can see the output that it stays the same hit part and position.
Meanwhile, if I aim at the entitie’s head or torso, it penetrates normally
Client script:
function bulletShoot()
gunSound:Play()
local origin = camera.CFrame.p
local direction = (mouse.Hit.p - origin).Unit
local count = 1
local spread = weaponData.SpreadDistance
local maxPenetration = weaponData.Penetration
local hitPart = {}
local lastParent = nil
local shootRay = RaycastParams.new()
shootRay.FilterDescendantsInstances = {camera, character, weaponCurrent, workspace.BulletWaste, hitPart}
shootRay.FilterType = Enum.RaycastFilterType.Blacklist
shootRay.IgnoreWater = true
run:BindToRenderStep('Shoot', Enum.RenderPriority.Camera.Value + 1, function(deltaTime)
local ray = workspace:Raycast(origin, direction * (weaponData.FireSpeed * deltaTime), shootRay)
if ray then
if ray.Instance.Parent:FindFirstChild('Humanoid') then
print(ray.Instance)
if ray.Instance.Parent == lastParent then
bulletHit:FireServer(ray.Instance, ray.Position, 0)
else
bulletHit:FireServer(ray.Instance, ray.Position, 1)
end
hitPart = ray.Instance
lastParent = ray.Instance.Parent
origin = ray.Position
count += 1
else
hitPart = ray.Instance
bulletHit:FireServer(ray.Instance, ray.Position, 0)
if ray.Instance.Name ~= 'Baseplate' then
local ray2 = workspace:Raycast(ray.Position, (ray.Position - camera.CFrame.p).Unit * 100, shootRay)
if ray2 then
local ray3 = workspace:Raycast(ray2.Position, (ray.Position - ray2.Position), shootRay)
if ray3 then
local wallThickness = math.abs((ray3.Position - ray.Position).Magnitude)
maxPenetration -= wallThickness
if maxPenetration > 0 then
bulletHit:FireServer(ray.Instance, ray3.Position, 0)
origin = ray3.Position
count += 1
else
run:UnbindFromRenderStep('Shoot')
end
end
end
else
run:UnbindFromRenderStep('Shoot')
end
end
elseif count < 180 then
origin = origin + direction
count += 1
else
run:UnbindFromRenderStep('Shoot')
end
end)
end
Server script:
bulletHit.OnServerEvent:Connect(function(player, object, position, dealDamage)
if object.Parent:FindFirstChild('Humanoid') then
object.Parent:FindFirstChild('Humanoid'):TakeDamage(50 * dealDamage)
end
local hole = Instance.new('Part')
hole.Parent = workspace:WaitForChild('BulletWaste')
hole.Position = position
hole.Size = Vector3.new(.1, .1, .1)
hole.Color = Color3.fromRGB(0, 0, 0)
hole.CanCollide = false
hole.Anchored = true
game:GetService('Debris'):AddItem(hole, 5)
end)
How can I fix this?