CLEO Help [LUA] Cant use any Loops in command, crashes

CLEO related

Skyfer

Member
Joined
Feb 4, 2022
Messages
18
Reaction score
1
Location
North Korea
how to add loop in a cmd code, my game crash when i add any kind of loop in the code like
while true do or repeat until
example:

Code:
function main()
  if not isSampLoaded() or not isSampfuncsLoaded() then return end
  while not isSampAvailable() do wait(100) end
  sampRegisterChatCommand("mycmd", cmd_mycmd)
  wait(-1)
end

function cmd_mycmd(arg)
  if #arg == 0 then

        sampAddChatMessage("{{FF0000}Empty", -1)       
        
  else
        sampAddChatMessage("{{FF0000}loop", -1)
        while true do
        sampAddChatMessage("{{FF0000}loop", -1)
        
        
        end
        

  end
end
 

Expl01T3R

Active member
Joined
Nov 20, 2022
Messages
133
Reaction score
21
Location
Czech Republic
well 'while' is kinda fast loop.. which can cause lag/game freezes during playing.. when u left it without sleep(delay_ms)..
and if ik good in last ugbase threads there was discussion about sleep in cmds.. u cant use sleep inside cmds.. if Iam right so maybe just make some function which will proceed your function and just call it in cmd ...
But maybe just forgot about this last one.. this is lua code so maybe i just know this thing from cleo post..
basically do sleep inside while cycle and u should be fine. or in your case sleep is wait() if i see good..
so basically code will look like this:

Code:
function main()
  while not isSampLoaded() or not isSampfuncsLoaded() or not isSampAvailable() do wait(100) end
  sampRegisterChatCommand("mycmd", cmd_mycmd)
end

function cmd_mycmd(arg)
  if #arg == 0 then
        sampAddChatMessage("{FF0000}Empty", -1)     
  else
        while true do
            sampAddChatMessage("{FF0000}loop", -1)
            wait(1000)
        end
  end
end
 

Halfastrc24

Active member
Joined
Jun 4, 2021
Messages
46
Solutions
1
Reaction score
6
Location
Greenland
Something like this?

Code:
function main()
    while not isSampAvailable() do wait(0) end

    sampRegisterChatCommand("cmd", cmd)
end

function cmd(arg)
    value = tonumber(arg)

    if value == nil then
        sampAddChatMessage('Empty', -1)
    else
        sampAddChatMessage('Loop Started', -1)
        lua_thread.create(function()
            while true do wait(0)
                sampAddChatMessage('Loop', -1)
                wait(5000)
            end
        end)
    end
end
 

Expl01T3R

Active member
Joined
Nov 20, 2022
Messages
133
Reaction score
21
Location
Czech Republic
but idk why you even want use loops in cmds.. atleast put them into thread.. to make it run smoothly.. this will send message every 1s after entering cmd with using some arg.. and every 1s game freezes..
 

Expl01T3R

Active member
Joined
Nov 20, 2022
Messages
133
Reaction score
21
Location
Czech Republic
Something like this?

Code:
function main()
    while not isSampAvailable() do wait(0) end

    sampRegisterChatCommand("cmd", cmd)
end

function cmd(arg)
    value = tonumber(arg)

    if value == nil then
        sampAddChatMessage('Empty', -1)
    else
        sampAddChatMessage('Loop Started', -1)
        lua_thread.create(function()
            while true do wait(0)
                sampAddChatMessage('Loop', -1)
                wait(5000)
            end
        end)
    end
end
exactly what iam talking about xD lua_thread.create well done.
 

Skyfer

Member
Joined
Feb 4, 2022
Messages
18
Reaction score
1
Location
North Korea
Thanks so much guys love you all
the codes you guys gave didnt work, but i did the create thread thing on the old code and it works


Code:
function main()
  if not isSampLoaded() or not isSampfuncsLoaded() then return end
  while not isSampAvailable() do wait(100) end
  sampRegisterChatCommand("mycmdtest", cmd_mycmd)
  wait(-1)
end

function cmd_mycmd(arg)
  if #arg == 0 then
     sampAddChatMessage('Empty', -1)
  else

    lua_thread.create(function()
        while true do wait(0)
            sampAddChatMessage('Loop', -1)
            wait(1000)   
        end
    end)


  end
end
 

Halfastrc24

Active member
Joined
Jun 4, 2021
Messages
46
Solutions
1
Reaction score
6
Location
Greenland
Actually to use 'wait' out the main function you need to use the 'lua_thread.create(function())'

Anyway, you're welcome =)
 
Top