How do I stop a script function after it has run once?

So I’ve done a script where someone touches a part it turns on music and such.
But after a certain amount of time I want it to completely stop and to do the same thing it has to be re-activated by being touched (Basically just want it to stop after the function has went once)

> -- Shortcuts --
> local Shortcut = game.Workspace
> -- Function --
> script.Parent.Touched:Connect(function()
> while true do
> Shortcut.GameTileMusic.Playing = true
> Shortcut.Map1.ArenaBillHolder.BillboardGui.TextLabel.Text = "Someone Has Entered the Arena!"
> wait(4)
> Shortcut.Border1.CanCollide = true
> Shortcut.Border2.CanCollide = true
> Shortcut.Border3.CanCollide = true
> Shortcut.Border4.CanCollide = true
> wait(4)
> Shortcut.Border1.CanCollide = false
> Shortcut.Border2.CanCollide = false
> Shortcut.Border3.CanCollide = false
> Shortcut.Border4.CanCollide = false
> end
> end)

I’ve tried adding a while true do loop inside the function but it kept doing the same thing, then I thought it must have something to do with values but I’m pretty inexperienced in scripting so I didn’t know if that’ll work or not.

If you want something to happen a number of times use a for loop. For example do to something 10 times:

for i=1, 10 do
    -- Your code goes here
end
2 Likes

One question, do I add that in the function or before that or in it?

It would be

for i=1, endNumber, increments do -- increments is optional, default is 1, so each loop it increases one
   -- function
end
2 Likes

Using for loops are the best way to get functions to run if you want it to run for a certain amount of loops!

I really would you ask to first learning Lua before start scripting it. This would really help you further if you understand what I mean. You even could learn things that you never learned before.

1 Like

I’ve learnt a little bit, that’s how I put up the script with the help of @notchloeno and@AvionicScript.
I will keep learning

2 Likes

Ok, good luck in the future!

(A while-true-loop never ends, this is what caused your error. In this case you even could remove the for-loop and the while-loop, you don‘t need it I think. If you need that this repeats x times, then use a for-loop yes. You can learn more about loops here:

)

Have a good day and keep learning!

1 Like

Scripting is how you learn Lua best! Trying to make stuff, failing, fixing it, rinse and repeat!

2 Likes