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

How do I make it so that whenever a player uses a tool, they have to wait some seconds?

Asked by 4 years ago

How do I make it so that whenever a player uses a tool, they get +1 leaderstat currency and they have to wait to use it again?

I have used this script

Tool = script.Parent -- Define your tool
Players = game:GetService('Players') 

Tool.Activated:Connect(function()
    local Player = Players:GetPlayerFromCharacter(Tool.Parent) 
    if Player then 
        if Player:FindFirstChild('leaderstats') and Player.leaderstats:FindFirstChild('Points') then
            Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + 1 
        end
    end
end)

But the players can just spam click and get 100 points a minute. So how do I add a cooldown to this script?

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You have to use a debounce, commonly known as a cooldown. This will make the code only run when the debounce is finished through conditional statements.

local Tool = script.Parent -- Define your tool
local Players = game:GetService('Players') 
local debounce = false

Tool.Activated:Connect(function()
    local Player = Players:GetPlayerFromCharacter(Tool.Parent)
    if debounce then -- Checks if the debounce is false
        if Player:FindFirstChild('leaderstats') and Player.leaderstats:FindFirstChild('Points') then
            debounce = true
            Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + 1 
            wait(2) -- You can change the wait time
            debounce = false 
        end
    end
end)

Also, you don't have to check if it's a player that's clicking as the player's mouse can only activate a tool. By the way, try using local variables instead of global ones as they can reduce some lag.

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You're going to need a remote event for this. First, insert a LocalScript into your tool, a RemoteEvent into ReplicatedStorage, and a Script in ServerScriptService. This is what you want to have in your scripts:

Local Script

-- This is going to fire your event when a player clicks while the tool is equipped.
local tool = script.Parent

tool.Activated:Connect(function()
    game.ReplicatedStorage.RemoteEvent:FireServer()
end)

Script

-- This is the event listener for when the event is fired, the server will take care of the points.

local tool = script.Parent
local debounce = false

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
    if debounce = false then
local player = game:GetService("Players"):GetPlayerFromCharacter(tool.Parent)
    if player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Points") then
        debounce = true
        player.leaderstats.Points.Value = player.leaderstats.Points.Value + 1
        debounce = false
        end
    end
end)

Hope this helps! If you have any questions, I'll try to reply back as soon as I can!

Answer this question