I have a self shell ejection cannon and it works great, the problem though is, while its reloading, if you continue to press the fire button (which is a GUI) the eject script will still continue to function thus cloning several casings while the gun is still reloading. I tried to disable the ejection script in the main script but it only ends up disabling it permanently. Here's the code
repeat wait() until script.Parent.Parent.Vehicle.Value~=nil db=false held=false v=script.Parent.Parent.Vehicle f=script.Parent missile=script.Parent.HE e=script.Parent.Eject function fireguns() v1=v.Value.Gun1Bullet:clone() vel=Instance.new("BodyVelocity") vel.Parent=v1 v1.Velocity=v1.CFrame.lookVector*500 v1.BodyVelocity.maxForce=Vector3.new(4000, 0, 4000) vel.velocity=v1.Velocity v1.CanCollide=true v1.Transparency=0 v1.Damage.Disabled=false v1.Parent=workspace end function launchmissile() v.Value.Effect.PointLight.Enabled = true v.Value.Effect.Smoke.Enabled = true v.Value.Effect.Fire:Play() v.Value.Effect.Transparency = .5 v.Value.WER2.Value.C0 = v.Value.WER2.Value.C0 - Vector3.new(0,0, .5) v1=v.Value.Missile:clone() v1.CFrame = v.Value.Missile.CFrame * CFrame.new(0, 0, -5) vel=Instance.new("BodyVelocity") vel.Parent=v1 v1.Velocity=v1.CFrame.lookVector*500 v1.BodyVelocity.maxForce=Vector3.new(4000, 0, 4000) vel.velocity=v1.Velocity v1.CanCollide=true v1.Transparency= 0 v1.Heat.Disabled=false v1.Parent=workspace wait(.1) v.Value.WER2.Value.C0 = v.Value.WER2.Value.C0 + Vector3.new(0,0, .1) v.Value.Effect.Transparency = .6 wait(.1) v.Value.WER2.Value.C0 = v.Value.WER2.Value.C0 + Vector3.new(0,0, .1) v.Value.Effect.Transparency = .7 wait(.1) v.Value.WER2.Value.C0 = v.Value.WER2.Value.C0 + Vector3.new(0,0, .1) v.Value.Effect.Transparency = .8 wait(.1) v.Value.WER2.Value.C0 = v.Value.WER2.Value.C0 + Vector3.new(0,0, .1) v.Value.Effect.Transparency = .9 wait(.1) v.Value.WER2.Value.C0 = v.Value.WER2.Value.C0 + Vector3.new(0,0, .1) v.Value.Effect.Transparency = 1 v.Value.Effect.PointLight.Enabled = false v.Value.Effect.Smoke.Enabled = false wait(.5) end function missileclick() print("click missile") if db==true then return end db=true missile.Text="Reloading" e.Disabled = true launchmissile() v.Value.Effect.Reload:Play() wait(3) missile.Text="Fire HEAT" v.Value.Effect.Reload:Stop() e.Disabled = false db=false end function gunsdown() held=true while held==true do fireguns() wait() end end function gunsup() held=false print("up") end missile.MouseButton1Click:connect(missileclick) sabot.MouseButton1Click:connect(sabotclick) guns.MouseButton1Down:connect(gunsdown) guns.MouseButton1Up:connect(gunsup)
Use something called a debounce
.
Debounces prevent scripts to trigger their function over and over again. Like in a touched script, like this one...
Workspace.Button.Touched:connect(function(hit) print("Button pressed") --Print the message wait(1) --Wait for 1 second print("Hi :D") --Print another message end)
You'd think this would only print
Button pressed Hi :D
but in reality It would say
Button pressed Button pressed Button pressed Button pressed Button pressed Hi :D Hi :D Hi :D Hi :D Hi :D
In order to stop that we need a debounce. I've fixed the script below.
repeat wait() until script.Parent.Parent.Vehicle.Value~=nil local debounce = false --DEBOUNCE HERE db=false held=false v=script.Parent.Parent.Vehicle f=script.Parent missile=script.Parent.HE e=script.Parent.Eject function fireguns() v1=v.Value.Gun1Bullet:clone() vel=Instance.new("BodyVelocity") vel.Parent=v1 v1.Velocity=v1.CFrame.lookVector*500 v1.BodyVelocity.maxForce=Vector3.new(4000, 0, 4000) vel.velocity=v1.Velocity v1.CanCollide=true v1.Transparency=0 v1.Damage.Disabled=false v1.Parent=workspace end function launchmissile() v.Value.Effect.PointLight.Enabled = true v.Value.Effect.Smoke.Enabled = true v.Value.Effect.Fire:Play() v.Value.Effect.Transparency = .5 v.Value.WER2.Value.C0 = v.Value.WER2.Value.C0 - Vector3.new(0,0, .5) v1=v.Value.Missile:clone() v1.CFrame = v.Value.Missile.CFrame * CFrame.new(0, 0, -5) vel=Instance.new("BodyVelocity") vel.Parent=v1 v1.Velocity=v1.CFrame.lookVector*500 v1.BodyVelocity.maxForce=Vector3.new(4000, 0, 4000) vel.velocity=v1.Velocity v1.CanCollide=true v1.Transparency= 0 v1.Heat.Disabled=false v1.Parent=workspace wait(.1) v.Value.WER2.Value.C0 = v.Value.WER2.Value.C0 + Vector3.new(0,0, .1) v.Value.Effect.Transparency = .6 wait(.1) v.Value.WER2.Value.C0 = v.Value.WER2.Value.C0 + Vector3.new(0,0, .1) v.Value.Effect.Transparency = .7 wait(.1) v.Value.WER2.Value.C0 = v.Value.WER2.Value.C0 + Vector3.new(0,0, .1) v.Value.Effect.Transparency = .8 wait(.1) v.Value.WER2.Value.C0 = v.Value.WER2.Value.C0 + Vector3.new(0,0, .1) v.Value.Effect.Transparency = .9 wait(.1) v.Value.WER2.Value.C0 = v.Value.WER2.Value.C0 + Vector3.new(0,0, .1) v.Value.Effect.Transparency = 1 v.Value.Effect.PointLight.Enabled = false v.Value.Effect.Smoke.Enabled = false wait(.5) end function missileclick() if not debounce then --CHECKS FOR DEBOUNCE debounce = true --Turns the debounce true to stop it from functioning again. print("click missile") if db==true then return end db=true missile.Text="Reloading" e.Disabled = true launchmissile() v.Value.Effect.Reload:Play() wait(3) missile.Text="Fire HEAT" v.Value.Effect.Reload:Stop() e.Disabled = false db=false debounce = false --wait 3 seconds to allow it to function again. end function gunsdown() held=true while held==true do fireguns() wait() end end function gunsup() held=false print("up") end missile.MouseButton1Click:connect(missileclick) sabot.MouseButton1Click:connect(sabotclick) guns.MouseButton1Down:connect(gunsdown) guns.MouseButton1Up:connect(gunsup)