I am trying to prevent touch from firing more then once, i have tried debouncing adding to tables and nothing is working to stop it.
function fireTorpedo(v) this.MainPart.FirePhoton:Play() local t = Instance.new("Part",workspace) t.CanCollide = true t.Anchored = false t.Size = Vector3.new(0.2,0.2,0.4) t.BrickColor = BrickColor.new("Really red") t.Material = "Neon" local m = Instance.new("SpecialMesh",t) m.MeshType = "Sphere" local l = Instance.new("PointLight",t) l.Color = Color3.new(1,0,0) local bv = Instance.new("BodyVelocity",t) local pos = (this.Model.TorpedoTube.CFrame*CFrame.new(3*torp_tube,0,-4)).p local dir = (v) and (v - pos).unit or this.Model.TorpedoTube.CFrame.lookVector t.BodyVelocity.velocity = this.MainPart.Velocity + dir*200 t.CFrame = CFrame.new(pos,pos + dir) torp_tube = -torp_tube t.Touched:connect(function(hit) local p3 = Instance.new("Part") p3.Transparency = 1 p3.Anchored = true p3.CanCollide = false p3.Size = Vector3.new(0.2,0.2,0.2) p3.Parent = game.Workspace p3.Position = t.Position game:GetService("Debris"):AddItem(t) game:GetService("Debris"):AddItem(p3, 0.75) local ex = Instance.new("Explosion") ex.BlastPressure = TorpedoBP ex.BlastRadius = TorpedoBR ex.DestroyJointRadiusPercent = 0 ex.Position = p3.Position ex.Parent = workspace local modelsHit = {} local HarmFireFX = this.MainPart.HarmFireFX:clone() HarmFireFX.Parent = p3 HarmFireFX.Enabled = true local HarmShotLight = this.MainPart.HarmShotLight:clone() HarmShotLight.Parent = p3 HarmShotLight.Enabled = true ex.Hit:Connect(function(part, distance) local parentModel = part.Parent if parentModel then if modelsHit[parentModel] then return end modelsHit[parentModel] = true local humanoid = parentModel:FindFirstChild("Humanoid") if humanoid then local distanceFactor = distance / ex.BlastRadius distanceFactor = 1 - distanceFactor humanoid:TakeDamage(TorpedoDamagePlayer * distanceFactor) end end end) local debounce = false t.Touched:Connect(function(hit) local modelsHitTorp = {} local parentModel = hit.Parent if parentModel then if modelsHitTorp[parentModel] then return end modelsHitTorp[parentModel] = true local VehicleHealth = parentModel:FindFirstChild("VehicleHealth") local GateshipShield = parentModel:FindFirstChild("GateshipShield") while debounce == false do debounce = true print ("True Debouncing") if VehicleHealth and not GateshipShield then print ("Hull Hit Torpedo") hit.Parent.VehicleHealth.Value = hit.Parent.VehicleHealth.Value - TorpedoDamageShuttle end wait(1) debounce = false print ("False Debouncing") end end end) local modelsHitTorp = {} local parentModel = hit.Parent if parentModel then if modelsHitTorp[parentModel] then return end local GateshipShield = parentModel:FindFirstChild("GateshipShield") local ShieldHealth = parentModel:FindFirstChild("ShieldHealth") if ShieldHealth and GateshipShield then print ("Shield Hit Torpedo") hit.Parent.ShieldHealth.Value = hit.Parent.ShieldHealth.Value - TorpedoDamageShuttle end end end) end
The error in your script is that you have a while loop with the condition being debounce must be false, and since you set the debounce to false right before the while loop ends, this results in a infinite while loop that causes the error you see now.
What you currently have:
while debounce == false do debounce = true print ("True Debouncing") if VehicleHealth and not GateshipShield then print ("Hull Hit Torpedo") hit.Parent.VehicleHealth.Value = hit.Parent.VehicleHealth.Value - TorpedoDamageShuttle end wait(1) debounce = false --this causes an infinite loop print ("False Debouncing") end
To be honest, I don't see why you're using a while loop, it is very unnecessary and is the cause of your problem. If you're worried about this script only working once, the multiple touched events you have will do that for you (I don't really understand why you have so many touched events, but you do you i guess). Anyways, a better way to do this would just be a regular if statement.
local debounce = false --make sure you define this OUTSIDE of the function (preferably at the top of the script) if not debounce then --basically checks if debounce is false debounce = true --code here end debounce = false --at the very end of the function or whatever to prevent it from repeating multiple times
Maybe try adding a variable that is equal to the amounts of times you want the function to be fired. For instance
local counter = 5 local function hit() if counter > 0 then --example code print(counter) counter = counter - 1 end end while true do wait(0.5) hit() end
When counter reaches 1 it stops. But maybe put the local counter = 5 variable outside of a while true loop or something. So that counter doesn't become 5 each time. If you don't have while true loops. And from reading your script you don't. Then this shouldn't be a problem. Just put the variable at the top of the script.