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

How to add a loop inside a function and break it in 2 secs?

I'm trying to add a while true do or repeat until loop inside this function but i also want to stop it in 2 seconds i know you can do somthing similar to a function for example

touch = part.Touched:Connect(function()
print "got it"
end)
wait (2)
touch:Disconnect()

im trying to go for something like this but with a loop it's hard ;/

here's my attempt

game.ReplicatedStorage:WaitForChild("shocky").OnServerEvent:Connect(function(plr)
	local s = script.s:Clone()
	script.shock:Play()
	s.Parent = plr.Character.Head
	local n = 0
	repeat print("doing code")  wait (1) n = n + 1 until n == 2
	wait (2)
	script.shock:Stop()
	s:Destroy()
end)

but it wont work because the wait(1) is gonna destroy the wait() loop

1 answer

✓ Accepted answer
   local n = 0
    while n < 2 do
		print("doing code")
		wait (1)
		n = n + 1
	end

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