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

How to make a loop that adds a value when part is clicked?

I'm currently making a challenge where you click a part a certain times to make it disappear. I tried to change it up but to no good. Here's what I thought, the loop is:

click = 0

while click < 10 do click = click + 1 end

I want to make it so that when I click on the part, it makes it "click = click + 1". But I tried different ways but haven't succeeded...

1 comment

0

You mean that it will loop adding value when you click on the part once? Or it adds one click when you press it once?

2 answers

From my understanding, you want the Click variable to keep adding 1 until it hits 10 once a ClickDetector is clicked.

So in order to do this, you can do:

local Clicked = 0

script.Parent.ClickDetector.MouseClick:connect(function()
	if Clicked < 10 then
		for i = 0,10,1 do
			Clicked = i
			if Clicked == 10 then
				--code here
			end
			wait(1)
		end
	end
end)

The for loop will start at 0(First number), it wants to end up at 10(middle number) and we are adding by 1(last number, a.k.a the increment), this is all happening per second(you can change how fast you want it to add by in the wait(n))

If I was wrong and you instead wanted to add 1 when its clicked then what you can do is:

local Clicked = 0

script.Parent.ClickDetector.MouseClick:connect(function()
	if Clicked < 10 then
		Clicked = Clicked + 1
		else
		print("Not less than 10, won't add.")
	end	
end)

Hope this helps :)

Alright, I've seen a couple things that could be improved on. I will show you how to do it, and then explain it. Please read the explanation to understand what I did.


REASON

  1. To click a Part you should be using a ClickDetector that detects when a part has been clicked instead of a while loop.
  1. You should be defining your variables as local instead of global.
local clicks = 0

NOT

clicks = 0
  1. You should also define where you want to start and where you want to stop.

  2. To make a part disappear you can either...

  1. Instead of wait() you should use RunService. (Explained in the Explanation)

SOLUTION

  • Part inside of the Workspace
  • ClickDetector inside of the Part
  • ServerScript (Regular Script) inside of the Part

Inside of the ServerScript

local startClicks = 0
local endClicks = 10

local part = script.Parent
local clickDetector = part.ClickDetector

local runService = game:GetService("RunService")

clickDetector.MouseClick:Connect(function(player)
	if startClicks < endClicks then
		startClicks = startClicks + 1 --// To simplify it, you can do startClicks += 1
	else
		startClicks = 0
		for num = 0,1,0.1 do
			part.Transparency = num
			runService.Stepped:Wait()
		end
	end
end)

EXPLANATION

  • First I started off of defining my variables
    • startClicks - Where we will start counting
    • endClicks - Where we will stop counting, AKA our goal.
    • part - Ease of access instead of doing script.Parent
    • clickDetector - ^
    • runService - Required to get 60 FPS instead of 30 FPS
      • wait() runs at 1/30th of a second, or 30 FPS
      • Stepped, RenderStepped, and Heartbeat run at 1/60th of a second, or 60 FPS
  • I then connected an Event Listener called MouseClick that listens for Click on the BasePart it's parented to. It also passes through the argument of the Player Instance that clicked the BasePart.
  • There is a simple if then statement that checks if the current number of clicks is less than the goal or not.
    • If it is, then we just increase it by one
    • if it isn't, then we hit the goal. We set it to 0, so if they do click again it won't re-call the for loop. We then create a for loop to start at 0 (Non-Transparent) to 1 (Transparent) by the incrementor, 0.1. Then set the part's transparency to the variable we set as num. I used runService.Stepped:Wait() as above, it's faster than wait() so the fade becomes more smooth.

If this helped you, don't forget to mark it as an answer!

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