local mapID = 1 local emeraldCount = 0 local startup = 1 -- Function to split strings (credits to SabreTheWolf) local function splitString(str, del) local arr = {} local last_num = 0 local copy = "" for i = 1, string.len(str) do local char = string.sub(str, i, i) if char == del then last_num = last_num + 1 arr[last_num] = copy copy = "" else copy = copy .. char end end last_num = last_num + 1 arr[last_num] = copy return arr end -- Saves the map ID and the number of emeralds to a file local function saveMapData(mapID, emeraldCount) local file = io.openlocal("client/mapinfo.dat", "w") if file then local data = mapID .. "," .. emeraldCount .. "\n" file:write(data) file:close() else print("Error: Could not open the file to save map data.") end end -- Function that executes when entering a new map local function onMapEnter() local mapID = gamemap local emeraldCount = emeralds if not G_IsSpecialStage(mapID) and G_GametypeUsesLives() then saveMapData(mapID, emeraldCount) end end -- Function to load map information from the file local function loadMapData() local file = io.openlocal("client/mapinfo.dat", "r") if file then local line = file:read("*line") file:close() if line and line ~= "" then -- The expected format for the line is: "mapID,emeraldCount" local data = splitString(line, ",") mapID = data[1] emeraldCount = tonumber(data[2]) or 0 return mapID, emeraldCount end end print("Pugging error of the damn hell") return nil, nil end -- this Funkction runs when the server starts to load saved information local function ServerStart() mapID, emeraldCount = loadMapData() if mapID then COM_BufAddText(nil, "map " .. mapID .. " -f") emeralds = emeraldCount -- Updates the global variable that manages the emeralds. print("\130Loaded map: " .. mapID .. " with " .. emeraldCount .. " emeralds.") else print("No saved map information found. Using default settings.") end end addHook("MapLoad", function () if titlemapinaction then return end if startup == 1 then ServerStart() startup = 0 end onMapEnter() end) addHook("GameQuit", function() startup = 1 end)