How create an allowed execution time in a custom loadstring

Hello! Im doing an fantasy computer that allows you to use lua to script programs, but i got a bug, when you create an infinite loop (aka while true) it crashes roblox. I use vLua, how i could set a allowed execution time so it stops after some time?

Good morning ! You can try while wait() do and set the duration you want (if you leave it so the delay will be reduced to a minimum)

This doesn’t solve the issue, what he wants is to be able to detect when a program is running for too long and stop said program.

1 Like

wait() is deprecated. I’d also recommend task.delay for making looping functions with no throttling, documentation here!

The coroutine library can be used to solve this guy’s issue. I’d recommend coroutine since its less performance intensive

2 Likes

Im almost reaching the answer thanks to you!
But can you send a example of doing that? I don’t use coro and task libs much.

You should get used to using these libraries regularly.

An example coroutine (from the developer hub):

local function task(...)
	-- This function might do some work for a bit then yield some value
	coroutine.yield("first")  -- To be returned by coroutine.resume()
	-- The function continues once it is resumed again
	return "second"
end
 
local taskCoro = coroutine.create(task)
-- Call resume for the first time, which runs the function from the beginning
local success, result = coroutine.resume(taskCoro, ...)
print(success, result)  --> true, first (task called coroutine.yield())
-- Continue running the function until it yields or halts
success, result = coroutine.resume(taskCoro)
print(success, result)  --> true, second (task halted because it returned "second")
1 Like