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

cooldown on OnTouched script how to?

Asked by 5 years ago
Edited 5 years ago

How to make this script have a cooldown?

local function AddAndKill(part)

local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent) -- This is the player

if player then

workspace.RedPoint.Value = workspace.RedPoint.Value + 1

player.Character.Humanoid.Health = 0

end

end

workspace.Touch.Touched:Connect(AddAndKill)

1 answer

Log in to vote
0
Answered by
Zeluxis 100
5 years ago
Edited 5 years ago

This can be done easily with a debounce code, which is basically a cooldown.

local function AddAndKill(part) local debounce=false

local player =

game:GetService(“Players”):GetPlayerFromCharacter(part.Parent) – This is the player

if player then debounce=true

workspace.RedPoint.Value = workspace.RedPoint.Value + 1

player.Character.Humanoid.Health = 0

end

end debounce=false workspace.Touch.Touched:Connect(AddAndKill)

Please note:

It's highly likely the above code does not work.. It's quite late and I don't have the mental capacity to run through your code and fix it, however I will briefly explain how a debounce is used.

A true or false statement can be used in many ways. To check if a door is open (local dooropen=false) for example, or in other ways, such as a cooldown.

So, for example, when a function is executed, check that debounce is false. If it is false, set it to true so it wont run again, and then after your code has completed itself, set debounce back to false (or after a timer) so that your code can run again.

Here's an example:

local debounce=false

script.Parent.MouseButton1.Clicked:Connect(function(Player) if debounce==false then debounce=true print("This works!") wait(3) debounce=false end end)

So, if you were to spam this button (click it many times), it would only print "This works!" every three seconds due to the debounce checker.

I hope this answer helped! If it did, please accept it as your answer!

Ad

Answer this question