Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to add a cool down to chat command?

Asked by 4 years ago

So this script here allows you to say !Explode and creates an explosion but you can spam it. Is there any way to stop this?

function onChatted(msg, speaker)
source = string.lower(speaker.Name)
msg = string.lower(msg)
if msg == "!explode" then
local boom = Instance.new("Explosion")
boom.Parent = game.Workspace
boom.Position = speaker.Character.UpperTorso.Position
boom.BlastPressure = 5
boom.BlastRadius = 10
end
end
function onPlayerEntered(newPlayer)
newPlayer.Chatted:connect(function(msg) onChatted(msg, newPlayer) end) 
end
game.Players.ChildAdded:connect(onPlayerEntered)

2 answers

Log in to vote
0
Answered by 4 years ago

I'm not exactly sure if you want it so you can't use it when you're dead or if you just want a cooldown, so I'll try my best to do both.

(Note: My method for making cooldowns is really bad, but my bad method works for this because the cooldown will atleast work on everyone.)

Can't explode on death:

-- this should work. i didnt test it though so im hoping it does work
function onChatted(msg, speaker)
source = string.lower(speaker.Name)
msg = string.lower(msg)
if msg == "!explode" then
local human = speaker.Character.Humanoid
if human.Health > 0 then
local boom = Instance.new("Explosion")
boom.Parent = game.Workspace
boom.Position = speaker.Character.UpperTorso.Position
boom.BlastPressure = 5
boom.BlastRadius = 10
end
end
end
function onPlayerEntered(newPlayer)
newPlayer.Chatted:connect(function(msg) onChatted(msg, newPlayer) end) 
end
game.Players.ChildAdded:connect(onPlayerEntered)

Cooldown for explosion (not just can't explode when dead):

-- hoping this also works
function onChatted(msg, speaker)
source = string.lower(speaker.Name)
msg = string.lower(msg)
if msg == "!explode" and not speaker.Character:FindFirstChild("debounceBoom") then

local value = Instance.new("NumberValue", speaker.Character) -- i use numbervalues since the time the debounce lasts for is the value
value.Name = "debounceBoom"
value.Value = 2 -- change to whatever u want
game:service("Debris"):AddItem(value, value.Value)

local boom = Instance.new("Explosion")
boom.Parent = game.Workspace
boom.Position = speaker.Character.UpperTorso.Position
boom.BlastPressure = 5
boom.BlastRadius = 10
end
end
function onPlayerEntered(newPlayer)
newPlayer.Chatted:connect(function(msg) onChatted(msg, newPlayer) end) 
end
game.Players.ChildAdded:connect(onPlayerEntered)

Hopefully any of these 2 work for you. :)

0
tysm i tried looking on the roblox website to see if i could try but couldnt so i truly thank you Funnyskyswagway3 30 — 4y
0
you're welcome youngmacka123 17 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Add a debounce which will act as cooldown, see this article on debounce

Answer this question