Home > Back-end >  Lua Sleep and Loop function problems
Lua Sleep and Loop function problems

Time:08-24

So my script currently stops on conditions

if profit >= 0.1 then
    stop()
end

i want to implement this code into mine so after the target satified it will sleep for 30 secs and then restart my dobet function

if found this

function sleep()
t0 = os.clock()
while os.clock() - t0 <= n do
      end

but i cant find a way to restart after sleep and restart my dobet() function

my code sample

https://pastebin.com/2Vi2iC81

CodePudding user response:

try:

local function sleep(seconds)
    local time = os.time()   seconds
    repeat until os.time() > time
end

chance = 49.5
multiplier = 2
basebet = 0.00000010
bethigh = false
​
function dobet()
    while true do
        if profit >= 0.1 then
            break
        end
    
        if win then
            nextbet = basebet
        else
            nextbet = previousbet * multiplier
        end
        sleep(30)
    end
end

this should fix it, let me know if it does not fix it.

CodePudding user response:

You can use goto to re-execute a code.

Example:

-- Use local variable instead using global variable, the global method can slowdown your script

local function sleep(s) -- Define a sleep function 
  local ntime = os.time()   s
  repeat until os.time() > ntime
end


-- Setup
local chance = 49.5
local multiplier = 2
local basebet = .00000010
local bethigh = false


-- Main field
local function dobet()
  if profit >= .1 then
    stop()
    
    sleep(30) -- Sleep/Wait for 30 seconds
    goto restart -- Go back to "restart" point
  end

  if win then
    nextbet = basebet

    sleep(30) -- Doing the same thing
    goto restart
  else
    nextbet = previousbet * multiplier

    sleep(30)
    goto restart
  end
end

::restart:: -- Define a "goto" point
dobet()

You can also use the factorial function method to re-run the function inside the function itself

local n = 0

local function printHelloWorld()
  if n >= 10 then
    return -- Stop the function basically
  else
    print('Hello World!')

    n = n   1
    printHelloWorld()
  end
end

printHelloWorld()

References:

  • Related