Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago

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

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

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

here's my attempt

01game.ReplicatedStorage:WaitForChild("shocky").OnServerEvent:Connect(function(plr)
02    local s = script.s:Clone()
03    script.shock:Play()
04    s.Parent = plr.Character.Head
05    local n = 0
06    repeat print("doing code")  wait (1) n = n + 1 until n == 2
07    wait (2)
08    script.shock:Stop()
09    s:Destroy()
10end)

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

1 answer

Log in to vote
1
Answered by 5 years ago
1local n = 0
2 while n < 2 do
3     print("doing code")
4     wait (1)
5     n = n + 1
6 end
Ad

Answer this question