Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My cooldown system of my rifle wont work, why?

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)

2 comments

0

I edited my answer for you. After reading my answer I am sure you can figure this out. I am not going to just spoonfeed you.

0

Glad I could help!

1 answer

✓ Accepted answer

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

How to disconnect an event.

5 comments

0

Doesn`t works, I editted my script, did I do something wrong? /\

0

Still wont work :C

0

After reading your question I realized that you wanted the gundisabled to prevent the player from firing when they are going through with an animation. I will not provide the entire script for you. I am sure that you can figure this out on your own after looking at my examples.

0

Nope, didnt work. I managed to fix it my self at the end. And yeah some of your tips were usefull since I needed the "canFire" be true and the "gundiabled" false.. but I found out I gotta make sure I check it AFTER I run a click function.

0

:(

This discussion is preserved as it was. New answers, comments, and votes are closed.