Scripting - Local to Global

Feb 22, 2026
1
0
50
15
Brazil
Pronouns
He
First of all, im new to scripting. Especially making Mario 64 mods and i edited this post just to be more "understandable".
Hello, i have a question, how can i make things like functions, hooks and other things like that, affecting to all people in the server?

For example:

LUA:
---@param m MarioState

function mario_update(m)

    if (m.controller.buttonPressed & L_TRIG) ~= 0 then

        warp_to_level(16,1,1)

    end

end

hook_event(HOOK_MARIO_UPDATE,mario_update)

Here we can see that, if i press L, I will warp to a level of number 16 (castle grounds if i remember well), BUT, only the LOCAL PLAYER (who pressed L) will warp.
And my question is, how i make that work on ALL PLAYERS in the server?
Thank you.
 
Last edited:
I haven't done modding for a long time, but I also just had this problem, and here's how I tackled it.

Your script works great to send one player there, but to send everybody, you need to communicate that to everyone.

Each player has a specific index in the server. The local player will always have a 0 for their index. There are various server lists that we can access that contain data for every player. For example, the gMarioStates array contains the m values for every single player in the lobby. We can loop through all these values using a for loop.

LUA:
for i = 0, (MAX_PLAYERS - 1) do
    -- Something here.
end

What this will do is it will start with the i variable at 0, and loop through all of the players - 1, since the player indices start at 0. In the loop, we need to change a value, but what value do we change that communicates to all players? We will use the gPlayerSyncTable. This table is a list of values that all of the players have and will be synchronized across all devices. If Player 2 tells Player 1's value in the gPlayerSyncTable to change, Player 1 will receive the change.

The plan here is to make one player change a value in the gPlayerSyncTable for ALL players, and each player will check if the value equals something. If it does, then we will warp them to the level and reset the value. Here's what a script would look like that fits into your system.

LUA:
---@param m MarioState

function mario_update(m)

    -- We only want the local player to run this script.
    if m.playerIndex ~= 0 then return end

    -- Storing the current state of the gPlayerSyncTable variable in a variable.
    -- We have to use m.playerIndex in the gPlayerSyncTable to make sure the right player receives it.

    local level_sender = gPlayerSyncTable[m.playerIndex].send_to_level


    -- If this value doesn't exist for the player yet, we create it.
    if level_sender == nil then
        gPlayerSyncTable[m.playerIndex].send_to_level = 0
        level_sender = 0
    end


    -- If we press L, begin the loop.

    if (m.controller.buttonPressed & L_TRIG) ~= 0 then


        -- By setting i to 0 and looping through all the players - 1, we loop through every player.

        for i = 0, (MAX_PLAYERS - 1) do

            -- We change every player's sync table value to 1, which tells them to warp to the level.
            gPlayerSyncTable[i].send_to_level = 1
        end

    end

    -- If someone has set the sync value to 1, then we set it back to 0 and warp to the level.

    if level_sender == 1 then
        gPlayerSyncTable[m.playerIndex].send_to_level = 0
        warp_to_level(16, 1, 1)
    end

end

hook_event(HOOK_MARIO_UPDATE,mario_update)

This script will loop through all players, set their communicated value to 1, and then for each player, it will check if that value is 1. If it is, it resets to 0 and warps them to the level. If you have any questions, please ask away. Have a great day.

- SkyCaster09
 

Users who are viewing this thread