Event Touched with For Loops

Can someone tell me why when I try to put a For loops inside a touched event nothing happens? my loop is not working.

Some code would be helpful to debug the situation.

1 Like

Show us your code, we can’t do anything without reference material.

Part1.Touched:Connect(function()
	for loops = 0, 10, 1 do
		print(loops)
	end
end)

Here’s an example of what I’d like to accomplish, when a person touches part1 a timer starts, except for now I just want to loop from 10 to 0

This will call a function that will run as a timer for you, if you want the timer to not take up time before moving on to the next part of the function when part is touched simply use a coroutine.wrap(function()).

local timerEnd = 20 -- this will make it last for 20 seconds (can change num to whatever you want)
local timePassed = 0 -- put it here so you can access it anywhere
local function timer()
  timePassed = 0
  while true do
    task.wait(1)
    timePassed += 1
    if timePassed >= timerEnd  then
      break --stops loop
    end
  end
end

Part1.Touched:Connect(function()
  timer()
  --below put whatever event you want to play after timer is done
end)

Can also use the tick function Tick() if you want to have it constantly there for you to check rather than checking every increment.

1 Like

Thank you very much, I tried before to code a timer except that the result was not there, thanks to you it works well thank you.

Instead of using a while loop you could still use the for loop @docega075
which would make it so you don’t have to break the loop nor have a specific variables

for i = 10, 0, -1 do -- set i = 10 for the starting point, 0 for the end point, and -1 is what it brings down the starting point until the end point
task.wait(1)
timer.Text = i
end

You were very close at your original post docega075, keep up the good work, sorry for the late response

Thank you very much for paying attention to my message.

1 Like

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