Hello, I am making a gun and I have a cooldown since its a rifle.. like M1 Garand. I also have another cooldown to prevent the player from shooting if he is lowering his weapon with an animation.
I have no idea why but my cooldowns wont work. It wont stop me from shooting but its a rifle so I need to have that small cooldown. Why wont this work?
canFire = true gundisabled = true --//-- SHOOTING --\\-- script.Parent.Equipped:Connect(function(Mouse) script.Parent.TakeOut:Play() if canFire == true then if gundisabled == true then Mouse.Button1Down:Connect(function() FireSound:Play() FlashPart.Material = "Neon" FlashPart.Transparency = 0 wait(0.1) FlashPart.Transparency = 1 canFire = false script.Parent.Smoke.ParticleEmitter.Enabled = true wait(3) canFire = true script.Parent.Smoke.ParticleEmitter.Enabled = false end) elseif gundisabled == false then print("Gun Is Disabled") elseif canFire == false then print("Rifle Cooldown") end end end)
The issue is that you never set canFire
equal to true
. Just throw that line in there right after you check if it is false and you should be good to go.
-- Example -- canFire = false gundisabled = false script.Parent.Equipped:Connect(function(Mouse) script.Parent.TakeOut:Play() if canFire == false then canFire = true -- Remember to change the value of your debounce after checking if gundisabled == false then Mouse.Button1Down:Connect(function() FireSound:Play() FlashPart.Material = "Neon" FlashPart.Transparency = 0 wait(0.1) FlashPart.Transparency = 1 canFire = true script.Parent.Smoke.ParticleEmitter.Enabled = true wait(3) canFire = false script.Parent.Smoke.ParticleEmitter.Enabled = false end) elseif gundisabled == true then print("Gun Is Disabled") elseif canFire == true then print("Rifle Cooldown") end end end)
The same applies for the gundisabled
variable. I hope this helps. Have a great day scripting!
Edit: You should really be doing the checking and changing of the variables inside of the Mouse1Down event. The reason your logic did not work is that you are only checking the debounce when the player equips the tool. Because you cannot get this to work, let me provide an example script.
canFire = true -- it makes more sense to have it true. Think about it. gundisabled = false script.Parent.Equipped:Connect(function(Mouse) script.Parent.TakeOut:Play() Mouse.Button1Down:Connect(function() if canFire and gunDisabled then canFire = false gunDisabled = true FireSound:Play() FlashPart.Material = "Neon" FlashPart.Transparency = 0 wait(0.1) FlashPart.Transparency = 1 script.Parent.Smoke.ParticleEmitter.Enabled = true wait(3) canFire = true gunDisabled = false script.Parent.Smoke.ParticleEmitter.Enabled = false end end) end)
Your question is not very clear so I am uncertain as to what gundisabled
is for. After seeing this example you should be able to figure out the logic on your own and decide how it is supposed to work. Here is an example of what your structure should look like:
-- create debounces script.Parent.Equipped:Connect(function() -- check if the gun is disabled -- if it is not disabled disable it and play the animation Mouse.Button1Down:Connect(function() if playerCanFire then -- if they can then disable canFire -- do shooting - wait and then enable canFire end end -- wait and then undisable the gun end -- I recommend that you turn off the Mouse.Button1Down event on unequip a link to how can be found below