[Question] About Wait Times/Cooldowns in luna.lua Scripts

Post here for help and support regarding LunaLua and SMBX2's libraries and features.

Moderator: Userbase Moderators

Superbloxen
Goomba
Goomba
Posts: 4
Joined: Tue Jun 28, 2022 6:46 pm

[Question] About Wait Times/Cooldowns in luna.lua Scripts

Postby Superbloxen » Sun Oct 13, 2024 8:22 pm

I recently tried to make a script while messing around in the editor, but realized that certain code would instantly repeat itself and cause the character to move at extremely high speeds for simple scripts that just add +1 to the character's height or speed. Is there any way to create a wait() command or other sort of cooldown to balance out scripts that do things like this?

WildW
Rocky Wrench
Rocky Wrench
Posts: 676
Joined: Thu Dec 14, 2017 2:21 pm
Flair: C# more like Db
Pronouns: he/him

Re: [Question] About Wait Times/Cooldowns in luna.lua Scripts

Postby WildW » Sun Oct 13, 2024 8:49 pm

Superbloxen wrote:
Sun Oct 13, 2024 8:22 pm
I recently tried to make a script while messing around in the editor, but realized that certain code would instantly repeat itself and cause the character to move at extremely high speeds for simple scripts that just add +1 to the character's height or speed. Is there any way to create a wait() command or other sort of cooldown to balance out scripts that do things like this?
You're not being very clear on what exactly you mean by "certain code", maybe try posting an example here and me or someone else can show you the intended way of solving that problem.

To broadly answer your question though, you can utilize the global function onTick, which is ran once for every game tick (~1/65th of a second) or use Routine.wait/Routine.waitFrames.

Superbloxen
Goomba
Goomba
Posts: 4
Joined: Tue Jun 28, 2022 6:46 pm

Re: [Question] About Wait Times/Cooldowns in luna.lua Scripts

Postby Superbloxen » Sun Oct 13, 2024 9:17 pm

Here's a snippet of the code in question:

Code: Select all

local MaxSpeedMinus = - 10
function onTick()
     if player.keys.up == KEYS_DOWN then
        if player.speedY > MaxSpeedMinus then
                   player.speedY = (player.speedY - 1) 
                   end
         end
end

I just wanted to add a short cooldown between speed increases so the character wouldn't just jump to max speed instantly when I held the key down.

Gaming-Dojo
Tweeter
Tweeter
Posts: 146
Joined: Mon Jan 25, 2016 9:20 am
Pronouns: he/him
Contact:

Re: [Question] About Wait Times/Cooldowns in luna.lua Scripts

Postby Gaming-Dojo » Sat Sep 06, 2025 4:21 pm

hello Superbloxen,

I would to that with a simple two variable timer. Here's also an example as I just coincidently used such a construction in my recent character select screen:

Code: Select all


local timer = 30 (whatever you want the delay to be)
local running = false

function yourfunc.onTick()
	
	if timer >= 0 then
		doStuff (change the speed in your case)
		timer = 30 (same value as above, resets the timer)
		running = false -- stops the timer
	end
	
	if running == true then
		doStuff (if you want, not necessary)
		timer = timer - 1
		doStuff (if you want, not necessary)
	end
	
	if timerShallStart (here goes what condition(s) you want to be met for the timer to start) then
		running = true
	else
		running = false
		timer = 30 (resetting timer to initial value so it doesn't start at a lower value if the condition for timer to run is met again)
	end
end


Return to “LunaLua Help”

Who is online

Users browsing this forum: No registered users and 1 guest

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari