Hello, I'm A Beginner In Scripting And I'm Making A Simulator Game And I'm Making A Script And I Don't Know How To Add A Cooldown In Here So Could Anyone Do It?
local Players = game:GetService("Players") local sword = game.ReplicatedFirst.ClassicSword local part = script.Parent local function onTouched(part) local player = Players:GetPlayerFromCharacter(part.Parent) if not player then return end local backpack = player.Backpack sword:Clone().Parent = backpack end part.Touched:Connect(onTouched)
If you want to add a cooldown to how many times a player can touch, then here is what I would do. Not sure if it will work, but it is good to give it a try!
local Players = game:GetService("Players") local sword = game.ReplicatedFirst.ClassicSword local cooldown = false -- Sets the variable cooldown to false local part = script.Parent local function onTouched(part) if cooldown == false then-- If cooldown is false then cooldown = true -- cooldown will turn into true local player = Players:GetPlayerFromCharacter(part.Parent) if not player then return end local backpack = player.Backpack sword:Clone().Parent = backpack wait(10) -- However long you want the cooldown time to be cooldown = false [[-- After all the above has runned, cooldown will return to false so it can be touched again!--]] end part.Touched:Connect(onTouched)
To make a cooldown with scripting, we use something called debounce
. First, you would assign a value that a player sticks with until they trigger a function and their value is temporarily changed until the function is completed. In order for the function to run, however, the player is required to have a specific value which is their original value. Here's a demonstration:
-- Let's say the script's parent is a TextButton local debounce = false -- the player starts out with false script.Parent.MouseButton1Click:Connect(function() if debounce == false then -- the player's debounce needs to be false; otherwise, the rest of the code in this if statement will not run debounce = true -- if the player clicks the button while their debounce is set to true, the code will not run because the if statement is checking if their debounce is false which it is not anymore script.Parent.Text = "Click accepted" else print("Cooldown is not over yet!") end wait(2) -- player has to wait 2 seconds until their debounce is set back to false again debounce = false -- now, they are able to repeat the function end)
Closed as Not Constructive by DesertusX, AntiWorldliness, imKirda, JesseSong, User#29913, TaxesArentAwesome, matiss112233, and killerbrenden
This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.
Why was this question closed?