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

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

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

Answer this question