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

How would I give this a cooldown?

Meaning, how can I... STOP people from spamming F in this Fireball script?

-------------------------------------Gunter5's Fireball Script----------------------------------------

wait(1)

Player = script.Parent.Parent -- Gets LocalPlayer 
Character = Player.Character -- Gets Players Character 
Mouse = Player:GetMouse() -- Gets players Mouse 
function onKeyDown(key) 
	key = key:lower() 
	if key == "f" then -- Press F To Shoot Fireball
		local x = Instance.new("Part")
		x.TopSurface = "Smooth" 
		x.BottomSurface = "Smooth" 
		x.Shape = "Ball" 
		x.Transparency = .7
		x.BrickColor = BrickColor.new("Bright red") 
		local z = Instance.new("Fire") 
		z.Parent = x 
		z.Size = 12 
		z.Heat = 8 
		local y = Instance.new("BodyVelocity") 
		y.maxForce = Vector3.new(math.huge, math.huge, math.huge)
		y.velocity = Character.Torso.CFrame.lookVector*80
		y.Parent = x  
		x.Parent = game.Workspace 
		x.CFrame = Character.Torso.CFrame*CFrame.new(0, 0, -12)
		local x = 1
		repeat
			wait(2)
			:remove(x)
			x = x + 1
		until x==0
	end
end

Mouse.KeyDown:connect(onKeyDown)

No, this is not mine, BUT!!! I've edited some things that were wrong with it to fix it.. so, how can I prevent people from spamming F?

1 answer

Assuming everything is working and functional the way it is, we can start adding to the script.

*Now before I get started, remember that the best way of creating good scripts is by making them yourself. When you copy off of someones model or video, you may not understand everything completely. *

Anyways, there's a super simple fix to your problem. The easiest way would be to create a **global **holding variable (one that's outside of an enclosing function). --Whenever the function is ran, we'll check if that holding variable is a certain amount (we'll say 0).

HoldVariable = 0 --This will be the new GLOBAL variable we create

function onKeyDown(key) 
    key = key:lower() 
    if key == "f" and HoldVariable == 0 then -- if our variable is equal to zero, the function will register
    	--do stuff
    end

If it is, than the click will be registered (function will be ran), but the variable will be changed to something other than 0 (1).

--This makes it so when ever the function is ran again, it will not register, because the variable is not 0.

HoldVariable = 0 --This will be the new GLOBAL variable we create

function onKeyDown(key) 
    key = key:lower() 
    if key == "f" and HoldVariable == 0 then -- if our variable is equal to zero, the function will register
	HoldVariable = 1 --This makes it so that if the function is called again, it will not register
    	--do stuff
    end

After a certain amount of time, we'll change our variable back to 0.

--So that when the function is called again, it will register that time.

HoldVariable = 0 --This will be the new GLOBAL variable we create

function onKeyDown(key) 
    key = key:lower() 
    if key == "f" and HoldVariable == 0 then -- if our variable is equal to zero, the function will register
	HoldVariable = 1 --This makes it so that if the function is called again, it will not register
    	--do stuff
	wait() --this will e our wait time until we change the variable back to 0
	HoldVariable = 0 --Now that we've waited, this allows us to now register function calls
    end

The final product should look like this:

wait(1)

Player = script.Parent.Parent -- Gets LocalPlayer 
Character = Player.Character -- Gets Players Character 
Mouse = Player:GetMouse() -- Gets players Mouse 
HoldVariable = 0
function onKeyDown(key) 
    key = key:lower() 
    if key == "f" and HoldVariable == 0 then -- Press F To Shoot Fireball
	HoldVariable = 1
        local x = Instance.new("Part")
        x.TopSurface = "Smooth" 
        x.BottomSurface = "Smooth" 
        x.Shape = "Ball" 
        x.Transparency = .7
        x.BrickColor = BrickColor.new("Bright red") 
        local z = Instance.new("Fire") 
        z.Parent = x 
        z.Size = 12 
        z.Heat = 8 
        local y = Instance.new("BodyVelocity") 
        y.maxForce = Vector3.new(math.huge, math.huge, math.huge)
        y.velocity = Character.Torso.CFrame.lookVector*80
        y.Parent = x  
        x.Parent = game.Workspace 
        x.CFrame = Character.Torso.CFrame*CFrame.new(0, 0, -12)
        local x = 1
        repeat
            wait(2)
            :remove(x)
            x = x + 1
        until x==0
	wait()
	HoldVariable = 0
    end
end

Mouse.KeyDown:connect(onKeyDown)

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