Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

how do i make this script have a cooldown instead of deleting itself?

i want to make this script have a cooldown instead of deleting itself after being touched, how would i go about making it do that?

local pointPart = script.Parent

-- Gives small points
local blue = Color3.fromRGB(0, 0, 255)
-- Gives large points
local green = Color3.fromRGB(0, 255, 0)
-- Makes players lose points
local red = Color3.fromRGB(255 ,0, 0)

-- points given to players
local smallPoints = 1
local largePoints = 3
local losePoints = 6

local Players = game:GetService("Players")

local function givePoints(player)
	local currentColor = pointPart.Color

	local playerStats = player:WaitForChild("leaderstats")
	local playerPoints= playerStats:WaitForChild("Points")

	-- Gives player points based on the color of the part
	if currentColor == blue then
		playerPoints.Value = playerPoints.Value + smallPoints	
	elseif currentColor == green then
		playerPoints.Value = playerPoints.Value + largePoints
	else
		playerPoints.Value = playerPoints.Value + losePoints	
	end

	pointPart:Destroy()
end

local function partTouched(otherPart)
	local player = Players:GetPlayerFromCharacter(otherPart.Parent)
	if player then
		givePoints(player)
	end
end

pointPart.Touched:Connect(partTouched)

1 comment

0

like with a timer or aslong as the script is running?

1 answer

Try this

local pointPart = script.Parent

local debounce = false

-- Gives small points
local blue = Color3.fromRGB(0, 0, 255)
-- Gives large points
local green = Color3.fromRGB(0, 255, 0)
-- Makes players lose points
local red = Color3.fromRGB(255 ,0, 0)

-- points given to players
local smallPoints = 1
local largePoints = 3
local losePoints = 6

local Players = game:GetService("Players")

local function givePoints(player)
  if debounce == false
   	 debounce = true
   	 local currentColor = pointPart.Color

   	 local playerStats = player:WaitForChild("leaderstats")
   	 local playerPoints= playerStats:WaitForChild("Points")

  	  -- Gives player points based on the color of the part
    	if currentColor == blue then
      	  playerPoints.Value = playerPoints.Value + smallPoints   
   	 elseif currentColor == green then
    	    playerPoints.Value = playerPoints.Value + largePoints
  	  else
       	 playerPoints.Value = playerPoints.Value + losePoints    
  	  end

		wait(3) -- Cooldown time
		debounce = false
	end
end

local function partTouched(otherPart)
    local player = Players:GetPlayerFromCharacter(otherPart.Parent)
    if player then
        givePoints(player)
    end
end

pointPart.Touched:Connect(partTouched)

This discussion is preserved as it was. New answers, comments, and votes are closed.