Lua help (button wise)

Noob2461

bad pfp on purpose
(No idea if this is in the right place or not but here goes)

Is there a way to make a button press instead of a hold? for example, the tutorial for changing Sonic's colour to red, you have to hold custom 1, but is it possible to make it just a button press? if so, what's the input?
 
There is no direct way to check if a button is simply being pressed instead of held. You can, however, increase a counter whenever the button is held, and reset it when released.

Press:
local function Test(p)
    if not (p.counter) then p.counter = 0 end
    if (p.cmd.buttons & BT_CUSTOM1) then
        p.counter = $ + 1
    else
        p.counter = 0
    end
    
    if (p.counter == 1) then --first frame of a hold, "a press"
        print("Henlo")
    end
end

Here, the print will only execute on the first frame of you holding Custom 1, and won't do anything afterwards.
 
So uhhh, what's wrong here? (please tell me in a basic way if at all possible)

Screenshot 2024-02-03 4.26.49 PM.png

Post automatically merged:

Better pic (when loading into srb2 it says there's an unexpected symbol near line 6 if)

Screenshot 2024-02-03 5.12.00 PM.png
Screenshot 2024-02-03 5.14.37 PM.png
 
Last edited:
If you get an error about an "unexpected symbol", it usually means you wrote something wrong: bad variable name, unclosed brackets, unclosed "if" statements etc.

Protip : Whenever you write an "if" statement, ALWAYS put the "end" to close it before you start writing the middle. It's like preparing the buns of a burger before you fill it up, prevents you from spilling tomato juice everywhere.

Lua:
local function turnNoobIsland(p)
    if (p.mo.skin) == "noob" then
        return
    end

    if p.cmd.buttons & BT_CUSTOM1 then
        if not p.pressedcustom1 then
            p.mo.color = SKINCOLOR_ISLAND
            p.pressedcustom1 = true
        end
    end
end

addHook("PlayerThink", turnNoobIsland)

Also, use the "PlayerThink" hook for this. Iterating over players with ThinkFrame was only a necessity back in 2014 because PlayerThink didn't exist, which honestly was a war crime.
 

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top