Tool:Activated() isnt working? I think

Inspired by a guy in my last topic I wanted to make a cooldown system. How its supposed to work is every time the tool is activated its meant to give the player +5 cash (under leaderstats) and it has a cooldown of 1 second. It isn’t working idk why. I’m still a beginner dev don’t judge

--LocalScript under Tool
local Player = game.Players.LocalPlayer
local ls = Player.leaderstats
local time = {}


script.Parent.Activated:Connect(function()
	time = os.time()

	if time and time - os.time() < 1000 then
		return("no")
	end

	if time and time - os.time() < 1000 then
		game.ReplicatedStorage.GiveCash:FireServer(5)
	end

	if not time then
		time = os.time()
	end
end)

Edit: Yes i know its not secure but Roblox introduced hyperion to mobile and UWP now so im not worried

1 Like

Can you show the server script

I fixed some of the code. It should work now, just make sure you have a RemoteEvent called GiveCash in ReplicatedStorage to handle giving the player cash and a leaderstats made. If it’s not working, then your server script might be causing issues.

-- LocalScript under Tool
local Player = game.Players.LocalPlayer
local ls = Player:WaitForChild("leaderstats")  -- Assuming leaderstats already exists

local lastActivationTime = 0  -- Initialize the last activation time to 0

script.Parent.Activated:Connect(function()
    local currentTime = os.time()

    -- Check if the cooldown time has passed (1 second)
    if currentTime - lastActivationTime >= 1 then
        -- Update the last activation time
        lastActivationTime = currentTime

        -- Fire a RemoteEvent to give the player +5 cash
        game.ReplicatedStorage.GiveCash:FireServer(5)
    end
end)
1 Like

Check the tool’s RequiresHandle property. if RequiresHandle is set to true and the tool doesn’t have a Handle, then .Activated() will not fire.

While I’m here, you could do the cooldown like this instead.

local deb = false --// Short for debounce
script.Parent.Activated:Connect(function()
    if deb then return end
    deb = true
    --// Code here
    wait(1)
    deb = false
end)

Will try it when I pick the PC up from repairs tmrw, thanks

1 Like

Server-side everything is working

Tool works alright it’s just that the cooldown system isn’t, will try that too tommorow

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.