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

Can while true do be used in a function?

I'm trying to make a script which when the brick is clicked it moves, although I don't know how to connect

while true do
	script.Parent.Position = script.Parent.Position + Vector3.new(0,0,0.1)
	wait(0.1)
end

script.Parent.ClickDetector.mouseClick:connect()-- I don't know what to put here.

In the () I normally put the name of the function I need to call. But when I use a how do I call the script?

Then I tried to do this

function move()
	script.Parent.Position = script.Parent.Position + Vector3.new(0,0,0.1)
	wait(0.1)
end

script.Parent.ClickDetector.mouseClick:connect(move)

But then it only moved once each time I clicked, but I'm not sure if you can put a while true do in a function?

3 comments

1

You most definitely can put a while loop inside a function, and anything else for that matter -- even other functions :)

0

Oh, thanks.

0

Be aware that if it's "while true do", your script will only exit the loop if you have a break statement inside it that executes.

1 answer

✓ Accepted answer

Yes, you can place anything in a function
If the boot fits...

Functions are just, for beginners, a convenient way to get code from another part of the script to run when you want it. This can be from an event, or when you call it yourself somewhere else in your code.

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