-
Howdy i’m trying to get the pierce function to work after the “defeated armor” variable is true, it is for sure becoming true but the pierce function isn’t doing it’s job, also i’m having another issue where the pierce function doesn’t work the first shot but works the second
-
https://gyazo.com/bbeb5cf191a914a5c9232fd7b1200256
https://gyazo.com/e288b10c8e52455bbfe289a01cca7d2b -
I have looked for solutions and have found no applicable advice, i have also tried to fix it for about 2 hours and have failed
I am quite new to coding and any advice would be appreciated.
TDLR
damage function checks armors blunt and sharp protection and then subtracts it agianst the bullets blunt and sharp damage and if the bullets damage values are higher it will penetrate issue being this is not happening
local tool = script.Parent
local fireevent = tool.fireevent
local reloadevent = tool.reloadevent
local FastCast = require(tool.FastCastRedux)
local firepoint = tool.Handle.Firepoint
local bulletsFolder = workspace:FindFirstChild("BulletFolder")
local casing = game:GetService("ServerStorage")["sketchy-rifle-casing"]
local LookVec = tool.exit_point.CFrame.LookVector
local Upvec = tool.exit_point.CFrame.UpVector
local bulletTemplate = bulletsFolder.bullet
local player = game.Players.LocalPlayer
--local idletrack = script.Parent.Handle.idle
local bulletdrop = -1000
local bulletspeed = 4000
local sharp = 35
local blunt = 20
local limbdmg = (sharp + blunt)
local ImpactParticle = tool.Handle.ImpactParticle
local defeatedarmor = false
local caster = FastCast.new()
local castParams = RaycastParams.new()
castParams.FilterType = Enum.RaycastFilterType.Blacklist
castParams.IgnoreWater = true
local castBehavior = FastCast.newBehavior()
castBehavior.RaycastParams = castParams
castBehavior.Acceleration = Vector3.new(0, bulletdrop , 0)
castBehavior.AutoIgnoreContainer = false
castBehavior.CosmeticBulletContainer = bulletsFolder
castBehavior.CosmeticBulletTemplate = bulletTemplate
castBehavior.CanPierceFunction = nil
FastCast.VisualizeCasts = true
local ammoleft = script.Parent:WaitForChild("max_mag").Value
local reloading = false
function spawnbulletcasing()
task.wait(0.25)
local casing = casing:Clone()
casing.CFrame = script.Parent.exit_point.CFrame
casing.Parent = workspace
local velocity = LookVec
casing.Velocity = velocity
end
function fleshimpact(sound)
local randomnumber = math.random(1,3)
if randomnumber == 1 then
sound.SoundId = "rbxassetid://11214662975" --flesh impact 1
sound:Destroy()
else
if randomnumber == 2 then
sound.SoundId = "rbxassetid://11214663995" -- flesh impact 2
sound:Destroy()
else
if randomnumber == 3 then
sound.SoundId = "rbxassetid://11214665296" -- flesh impact 3
sound:Destroy()
end
end
end
end
function metalimpact(sound)
local randomnumber = math.random(1,3)
if randomnumber == 1 then
sound.SoundId = "rbxassetid://11214495144" --metal impact 1
sound:Destroy()
else
if randomnumber == 2 then
sound.SoundId = "rbxassetid://11214496337" -- metal impact 2
sound:Destroy()
else
if randomnumber == 3 then
sound.SoundId = "rbxassetid://11214537030" -- metal impact 3
sound:Destroy()
end
end
end
end
function MakeParticleFX(position, normal)
local attachment = Instance.new("Attachment")
attachment.CFrame = CFrame.new(position, position + normal)
attachment.Parent = workspace.Terrain
local sound = Instance.new("Sound")
sound.Parent = attachment
sound.PlayOnRemove = true
sound.RollOffMaxDistance = 100
sound.RollOffMinDistance = 10
sound.Volume = 1
metalimpact(sound)
local emitter = Instance.new("ParticleEmitter")
emitter.Parent = attachment
emitter.SpreadAngle = Vector2.new(360, 360)
emitter.Rate = 350
emitter.Speed = NumberRange.new(200,200)
emitter.Squash = NumberSequence.new(5,5)
emitter.Orientation = 2
emitter.Rotation = NumberRange.new(90)
local white = Color3.new(1, 0.662745, 0.113725)
local lightBlue = Color3.new(0, 2/3, 1)
local yellow = Color3.new(1, 1, 0)
local cs = ColorSequence.new{
ColorSequenceKeypoint.new(0, white),
ColorSequenceKeypoint.new(1, white)
}
emitter.Color = cs
emitter.Transparency = NumberSequence.new(0.5,0.5)
emitter.LightEmission = 1
emitter.Lifetime = NumberRange.new(0.025,0.025)
wait(0.1)
emitter.Enabled = false
end
local function Pierce(cast, result, velocity)
---checking if the armor defeated variable is correct
print("armor defeated?",defeatedarmor)
local Pierce_able = false
if result and result.Instance then
if result.Instance:GetAttribute("penable") then
Pierce_able = true
--everything else works
print("penned:",result)
else
if result.Instance:GetAttribute("HP") then
local varhit = result.Instance
local sound = Instance.new("Sound")
sound.Parent = result.Instance
sound.PlayOnRemove = true
sound.RollOffMaxDistance = 100
sound.RollOffMinDistance = 10
sound.Volume = 1
fleshimpact(sound)
Pierce_able = true
varhit:SetAttribute("HP", varhit:GetAttribute("HP") - sharp, blunt)
else
if defeatedarmor == true then
--code messes up here, when the defeated armor variable is true the code runs but the bullet stops and doesn't pierce
print("pierced armor!")
Pierce_able = true
defeatedarmor = false
else
Pierce_able = false
end
return Pierce_able
end
end
end
end
local function damage(varhit, cast, result, velocity)
if varhit:GetAttribute("HP") then
varhit:SetAttribute("HP", varhit:GetAttribute("HP") - sharp, blunt)
else
if varhit:GetAttribute("bluntprot") or varhit:GetAttribute("sharpprot") then
varhit:SetAttribute("bluntprot", varhit:GetAttribute("bluntprot") - blunt)
varhit:SetAttribute("sharpprot", varhit:GetAttribute("sharpprot") - sharp)
print("bluntprot",varhit:GetAttribute("bluntprot"))
print("sharpprot",varhit:GetAttribute("sharpprot"))
if varhit:GetAttribute("sharpprot") < 0 then
print("sharp penned")
defeatedarmor = true
castBehavior.CanPierceFunction = Pierce
local HitPierce = Pierce(cast, result, velocity)
end
if varhit:GetAttribute("bluntprot") < 0 then
print("blunt penned")
defeatedarmor = true
castBehavior.CanPierceFunction = Pierce
local HitPierce = Pierce(cast, result, velocity)
end
end
end
end
local function reload()
if reloading then return end
reloading = true
task.wait(0.25)
tool.stock.reloadnoise:play()
task.wait(3.25)
ammoleft = script.Parent["max_mag"].Value
reloading = false
end
local function onEquipped()
castParams.FilterDescendantsInstances = {tool.Parent, bulletsFolder,}
end
local function onLengthChanged(cast, lastPoint, direction, length, velocity, bullet)
if bullet then
local bulletLength = bullet.Size.Z/2
local offset = CFrame.new(0, 0, -(length - bulletLength))
bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)
end
end
local function onRayHit(cast, result, velocity, bullet)
local hit = result.Instance
local hitPoint = result.Position
local normal = result.Normal
local character = hit:FindFirstAncestorWhichIsA("Model")
--print("hit flesh")
print(hit)
MakeParticleFX(hitPoint,normal)
if hit:GetAttribute("penable")then
castBehavior.CanPierceFunction = Pierce
local HitPierce = Pierce(cast, result, velocity)
else
if hit:GetAttribute("HP") then
local sound = Instance.new("Sound")
sound.Parent = hit
sound.PlayOnRemove = true
fleshimpact(sound)
game:GetService("Debris"):AddItem(bullet,3)
castBehavior.CanPierceFunction = Pierce
local HitPierce = Pierce(cast, result, velocity)
else
if hit:GetAttribute("bluntprot") or hit:GetAttribute("sharpprot") then
local varhit = hit
damage(varhit,cast, result, velocity)
game:GetService("Debris"):AddItem(bullet,0.025)
else
game:GetService("Debris"):AddItem(bullet,0.025)
end
end
end
end
local function fire(player,mousepos)
if tool.Parent:IsA("Backpack") then return end
if ammoleft > 0 and not reloading then
firepoint.PointLight.Enabled = true
firepoint.ParticleEmitter.Enabled = true
local orign = firepoint.WorldPosition
local direction = (mousepos - orign).Unit
tool.stock.firenoise:play()
caster:Fire(orign, direction, bulletspeed, castBehavior)
spawnbulletcasing()
ammoleft -= 1
firepoint.PointLight.Enabled = false
firepoint.ParticleEmitter.Enabled = false
else
reload()
end
end
reloadevent.OnServerEvent:Connect(reload)
fireevent.OnServerEvent:Connect(fire)
tool.Equipped:Connect(onEquipped)
caster.LengthChanged:Connect(onLengthChanged)
caster.RayHit:Connect(onRayHit)