I'm working on destructible barriers in my game. I'm not done with all the weapons yet, but my current ones shoot out a bullet called "basicbullet". The walls are supposed to lose exactly one hitpoint every time they're hit, however, using the touched event isn't very reliable as sometimes the wall might lose more than 1 and sometimes will fail to detect the bullet.
Here's a clip showing the speed of the bullet (it goes pretty fast): https://gyazo.com/390b3cc8bdc6cc1fe103101972e03af1
Here's the wall script: Note that only the first few lines are hit detection, the rest of it is just the smoke effect when it loses enough health.
local CollectionService = game:GetService("CollectionService") local parttodestroy = CollectionService:GetTagged("WoodWall") for _, TaggedPart in pairs(parttodestroy) do local model = TaggedPart local originx = model.Position.x local originy = model.Position.y local originz = model.Position.z local enabled = true local hitpoints = 18 local maxhitpoints = 18 local crithit = maxhitpoints/2.5 local emitter = Instance.new("ParticleEmitter") emitter.Enabled = false local location = "none" local checkfora = (model.Position - workspace.FlagA.Hitbox.Position).Magnitude if checkfora < 250 then location = "a" end TaggedPart.Touched:Connect(function(hit) if enabled == true then if hit.Name == "basicbullet" then hitpoints = hitpoints - 1 end if location == "a" then workspace.FlagA.BaseHealth.Value = workspace.FlagA.BaseHealth.Value - 1 end print(hitpoints) print(workspace.FlagA.BaseHealth.Value) if hitpoints <= crithit then emitter.Rate = 75 emitter.Lifetime = NumberRange.new(1.7, 2.5) emitter.Enabled = true emitter.Texture = "rbxassetid://405886187" local colorKeypoints = { ColorSequenceKeypoint.new(0, Color3.new(0.945098, 0.788235, 0)), ColorSequenceKeypoint.new(0.1, Color3.new(1, .5, 0)), ColorSequenceKeypoint.new(0.5, Color3.new(1, 0, 0)), ColorSequenceKeypoint.new(1, Color3.new(0, 0, 0)) } emitter.Color = ColorSequence.new(colorKeypoints) local numberKeypoints = { NumberSequenceKeypoint.new(0, 0); NumberSequenceKeypoint.new(0.3, 0.25); NumberSequenceKeypoint.new(0.5, 0.4); NumberSequenceKeypoint.new(1, 1); } emitter.Transparency = NumberSequence.new(numberKeypoints) emitter.LightEmission = 0.30 emitter.EmissionDirection = Enum.NormalId.Front emitter.Speed = NumberRange.new(0, 1) emitter.Drag = 1 emitter.VelocitySpread = NumberRange.new(0, 0) emitter.VelocityInheritance = 0 emitter.Acceleration = Vector3.new(0, 10, 0) emitter.LockedToPart = false emitter.SpreadAngle = Vector2.new(-10,10) local numberKeypoints2 = { NumberSequenceKeypoint.new(0, 1); NumberSequenceKeypoint.new(1, 0.7); } emitter.Size = NumberSequence.new(numberKeypoints2) emitter.Rotation = NumberRange.new(15, 15) emitter.RotSpeed = NumberRange.new(10, 20) emitter.Parent = TaggedPart end if hitpoints <= 0 then emitter.Enabled = false enabled = false model.Transparency = 1 model.CanCollide = false model.Position = Vector3.new(model.Position.x, -50, model.Position.z) wait(20) model.Position = Vector3.new(originx, originy, originz) model.Transparency = 0 model.CanCollide = true enabled = true hitpoints = 18 end end end) end
What would be the most reliable way to reduce the hitpoints by exactly 1 every time the wall is hit by a projectile? Thanks for your help :)