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

Can I make this script have a cooldown?

This script can be spammed and it's annoying. When I try to add a cooldown to it, it breaks. I want it to at least wait 10 seconds before it can be shot again. Help please

Script:

Player = script.Parent.Parent
mouse = Player:GetMouse()
 



function onKeyDown(key)
local Key = key:lower()
if key == "z" and Player.Levelss.Level.Value >= 15  then
local x = Instance.new("Part")
x.BrickColor = BrickColor.new("White")
x.Material = "Neon"
x.CanCollide = false
x.Transparency = 0.6
x.Size = Vector3.new(10,10,10)
x.TopSurface = "Smooth"
x.BottomSurface = "Smooth"
x.Shape = "Ball"
local fd = script.fireDamage:clone()
fd.Parent = x
local a = Instance.new("Sound")
        a.SoundId = "http://www.roblox.com/asset/?id=260430117"
        a.Parent = x
        a.Volume = 2
        a:play()




local y = Instance.new("BodyVelocity")
y.maxForce = Vector3.new(math.huge ,math.huge ,math.huge)
y.velocity = Player.Character.Torso.CFrame.lookVector*95

x.CFrame = Player.Character.Torso.CFrame*CFrame.new(0, 0 , -12)
x.Parent = Workspace
y.Parent = x	
game.Debris:AddItem(x, 6)
	
	
end
end


mouse.KeyDown:connect(onKeyDown)

Thanks for reading

1 answer

✓ Accepted answer

The tick function returns the amount of time since the unix epoch (January 1 1970). Using this information, the fact that time changes steadily, and some simple math, a cooldown can be implemented into your script.

local Player = game.Players.LocalPlayer --// I assume this is a LocalScript, so you can access the player this way.
local mouse = Player:GetMouse() --// It's a good practice to use local variables.
local lastUse = 0;

local function onKeyDown(key) --// And local functions.
	local Key = key:lower()
	if key == "z" and Player.Levelss.Level.Value >= 15 and tick()-lastUse > 10  then --// Check if it's been ten seconds since the last use.
		local x = Instance.new("Part")
		x.BrickColor = BrickColor.new("White")
		x.Material = "Neon"
		x.CanCollide = false
		x.Transparency = 0.6
		x.Size = Vector3.new(10,10,10)
		x.TopSurface = "Smooth"
		x.BottomSurface = "Smooth"
		x.Shape = "Ball"
		local fd = script.fireDamage:clone()
		fd.Parent = x
		local a = Instance.new("Sound")
		a.SoundId = "http://www.roblox.com/asset/?id=260430117"
		a.Parent = x
		a.Volume = 2
		a:play()
		
		
		local y = Instance.new("BodyVelocity")
		y.maxForce = Vector3.new(math.huge ,math.huge ,math.huge)
		y.velocity = Player.Character.Torso.CFrame.lookVector*95
		
		x.CFrame = Player.Character.Torso.CFrame*CFrame.new(0, 0 , -12)
		x.Parent = Workspace
		y.Parent = x    
		game.Debris:AddItem(x, 6)
		
		lastUse = tick(); --// Set the last use to the current time.
	end
end


mouse.KeyDown:connect(onKeyDown)

I'd recommend converting your code to use UserInputService, instead; mouse.KeyDown is deprecated.

Hope this helped.

PS: See how much more readable your code is when it's indented correctly? I urge you to check out these wiki pages about clean code and indenting. Good luck!

2 comments

0

Thank you! I know that I'm suppose to clean up my code, I just don't do it until I'm sure I've finished the script

0

No problem.

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