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

How would I make my script have a debounce?

My script makes a NPC say something when a proximity prompt is run, but I want a debounce. I am not too familiar with debounces, but I feel like I could use some help.

I know I could use HoldDuration, but that could break upcoming scripts that may be added.

This is my current script:

local Chat = game:GetService("Chat")
local messages = {
	"Welcome to Bloxy Mart!",
	"Hello and welcome to Bloxy Mart!",
	"Salutations, welcome to Bloxy Mart!",
	"Having a fine day? Welcome to Bloxy Mart!"
}

script.Parent.Triggered:Connect(function(playerWhoTriggered)
	Chat:Chat(script.Parent.Parent.Parent.Head, messages[math.random(#messages)])
end)

Any help to add a debounce? If so please answer this question. Thank you!

2 comments

0

If you're not going to use "playerWhoTriggered", then I suggest removing the parameter.

1 answer

✓ Accepted answer

add these into the script:

local debounce = false -- new variable

local Chat = game:GetService("Chat")
local messages = {
    "Welcome to Bloxy Mart!",
    "Hello and welcome to Bloxy Mart!",
    "Salutations, welcome to Bloxy Mart!",
    "Having a fine day? Welcome to Bloxy Mart!"
}

script.Parent.Triggered:Connect(function(playerWhoTriggered)
    if debonce == false then -- check
	    debounce == true --set it so it wont work
	    Chat:Chat(script.Parent.Parent.Parent.Head, messages[math.random(#messages)])
	    wait() -- wait
            debounce == false -- works again
    end
end)

2 comments

0

"==" is the equality operator, you cannot use it to make assignments. "wait" has also been superseded by "task.wait". On top of that, you should be specifying a duration.

1

Looks like it works! Thanks for helping, I learnt something new as always. :)

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