Problem with Money Giving when part touched without cooldown

Ive been thinking so hard I feel like my brain is about to shut down… I think im just overthinking this but I need help. It seems so simple. I need help adding a cooldown. So pretty much when the GUI pops up, you step on the part and it gives you cash. But…Ive tried adding cooldowns, but it never works.



if game.StarterGui.SchoolTask.TextLabel.TextLabel_Roundify_12px:TweenPosition(UDim2.new(0.404, 0,0.558, 0),"InOut","Sine",1) then
	function onTouched(hit)
			if hit.Parent.Humanoid~= nil then
				local cash = game.Players:findFirstChild(hit.Parent.Name).leaderstats.Cash 
			 cash.Value = cash.Value + 10 
	
		
			end
		end
	end
	

script.Parent.Touched:connect(onTouched) 
if script.Parent.Touched:connect(onTouched)then
	wait(1440)
end

I assume you’ve added wait(1440) to add a cooldown, but unfortunately, this will not affect anything on the script if wait() function is written alone.

In order to add a proper cooldown, best practice is to add a boolean variable that indicates if it’s currently on cooldown or not.

It would be looking like this:

local Cooldown = false

if game.StarterGui.SchoolTask.TextLabel.TextLabel_Roundify_12px:TweenPosition(UDim2.new(0.404, 0,0.558, 0),"InOut","Sine",1) and not Cooldown then
	function onTouched(hit)
		if hit.Parent.Humanoid~= nil then
			local cash = game.Players:findFirstChild(hit.Parent.Name).leaderstats.Cash 
			cash.Value = cash.Value + 10 
		end
	end
end


script.Parent.Touched:connect(onTouched) 
if script.Parent.Touched:connect(onTouched)then
	Cooldown = true
	wait(1440)
	Cooldown = false
end
1 Like

i don’t think the second event isn’t gonna work but why don’t you use a variable like local cooldown = false or something like that?

here’s a basic example:

local part = script.Parent -- assuming that script.Parent is the part
local cooldown = false -- here's our cooldown

local waitingTime = 10 -- this is how long the cooldown should last for

part.Touched:Connect(function(hit) -- use :Connect instead of :connect since the lower-cased one is deprecated
    if hit.Parent:FindFirstChild("Humanoid") then -- this checks if the part that touched the part that gives a player money is a character
        if not cooldown then -- this checks if it should give money
            cooldown = true -- this stops the part from giving a player money

            local player = game.Players:FindFirstChild(hit.Parent.Name) -- this looks for the player
            local leaderstats = player.leaderstats -- this looks for the folder called "leaderstats"
            local cash = leaderstats.Cash -- this looks for a value called "Cash"

            if cash then -- checks if cash exists or not
               cash.Value += 10 -- gives player +10 money!
            end

            wait(waitingTime)
            
            cooldown = false -- this sets it back to false, allowing the part to give money again
        end
    end
end)

also, you’re not looking at the player’s GUI, so change game.StarterGui to player.PlayerGui if you are looking to see if the player has the GUI or not

1 Like

Thank you so much! :smiley: I forgot you could simply just make a variable for the cooldown lol

I just tried your script, and it doesn’t work.