fadhil_297

Untitled

Jul 26th, 2025
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.82 KB | Source Code | 0 0
  1. --[[ hi i think this script is works very well
  2.     bc it never failed to save and load
  3. ]]
  4. local DataStoreService = game:GetService("DataStoreService")
  5. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  6. local ServerScriptService = game:GetService("ServerScriptService")
  7. local Players = game:GetService("Players")
  8. local HttpService = game:GetService("HttpService")
  9. local remoteload = ReplicatedStorage:WaitForChild("LoadSign")
  10.  
  11. -- Constants
  12. local MAX_DATA_SIZE = 3900000 -- ~3.9MB (slightly under DataStore's 4MB limit)
  13. local MAX_DATASTORES = 1000
  14. local MAX_CHUNKS_PER_STORE = 5
  15. local isMapLoading = false
  16. local saveQueue = {}
  17. local mapFullyLoaded = false
  18.  
  19. -- Helper functions for data conversion
  20. local function vector3ToTable(vec)
  21.     return {vec.X, vec.Y, vec.Z}
  22. end
  23.  
  24. local function tableToVector3(tbl)
  25.     return Vector3.new(tbl[1], tbl[2], tbl[3])
  26. end
  27.  
  28. local function color3ToTable(color)
  29.     return {color.R, color.G, color.B}
  30. end
  31.  
  32. local function tableToColor3(tbl)
  33.     return Color3.new(tbl[1], tbl[2], tbl[3])
  34. end
  35.  
  36. local function udim2ToTable(udim)
  37.     return {
  38.         {udim.X.Scale, udim.X.Offset},
  39.         {udim.Y.Scale, udim.Y.Offset}
  40.     }
  41. end
  42.  
  43. local function tableToUDim2(tbl)
  44.     return UDim2.new(tbl[1][1], tbl[1][2], tbl[2][1], tbl[2][2])
  45. end
  46.  
  47. local function verifyPartAnchoring()
  48.     local parts = {}
  49.     local unanchoredCount = 0
  50.  
  51.     for _, obj in ipairs(workspace:GetDescendants()) do
  52.         if obj:IsA("BasePart") and not obj:IsDescendantOf(game:GetService("Players")) then
  53.             table.insert(parts, obj)
  54.         end
  55.     end
  56.  
  57.     for _, part in ipairs(parts) do
  58.         if not part.Anchored and not part:GetAttribute("Physics") and part:GetAttribute("ShouldBeAnchored") then
  59.             part.Anchored = true
  60.             unanchoredCount += 1
  61.         end
  62.     end
  63.  
  64.     if unanchoredCount > 0 then
  65.         print(`-- Fixed {unanchoredCount} unanchored parts`)
  66.     end
  67. end
  68.  
  69. local function serializeGuiObject(obj)
  70.     if obj:IsA("TextLabel") then
  71.         return {
  72.             ClassName = "TextLabel",
  73.             Name = obj.Name,
  74.             Text = obj.Text,
  75.             TextColor3 = color3ToTable(obj.TextColor3),
  76.             BackgroundColor3 = color3ToTable(obj.BackgroundColor3),
  77.             TextSize = obj.TextSize,
  78.             Font = obj.Font.Name,
  79.             Position = udim2ToTable(obj.Position),
  80.             Size = udim2ToTable(obj.Size),
  81.             BackgroundTransparency = obj.BackgroundTransparency,
  82.             TextTransparency = obj.TextTransparency,
  83.             TextXAlignment = obj.TextXAlignment.Name,
  84.             TextYAlignment = obj.TextYAlignment.Name,
  85.             TextWrapped = obj.TextWrapped,
  86.             TextScaled = obj.TextScaled,
  87.             BorderSizePixel = obj.BorderSizePixel,
  88.             BorderColor3 = color3ToTable(obj.BorderColor3),
  89.             Visible = obj.Visible,
  90.             Children = {}
  91.         }
  92.     elseif obj:IsA("Frame") then
  93.         local frameData = {
  94.             ClassName = "Frame",
  95.             Name = obj.Name,
  96.             BackgroundColor3 = color3ToTable(obj.BackgroundColor3),
  97.             Position = udim2ToTable(obj.Position),
  98.             Size = udim2ToTable(obj.Size),
  99.             BackgroundTransparency = obj.BackgroundTransparency,
  100.             BorderSizePixel = obj.BorderSizePixel,
  101.             BorderColor3 = color3ToTable(obj.BorderColor3),
  102.             Visible = obj.Visible,
  103.             Children = {}
  104.         }
  105.  
  106.         for _, child in ipairs(obj:GetChildren()) do
  107.             local childData = serializeGuiObject(child)
  108.             if childData then
  109.                 table.insert(frameData.Children, childData)
  110.             end
  111.         end
  112.         return frameData
  113.     elseif obj:IsA("ImageLabel") then
  114.         return {
  115.             ClassName = "ImageLabel",
  116.             Name = obj.Name,
  117.             Image = obj.Image,
  118.             ImageColor3 = color3ToTable(obj.ImageColor3),
  119.             BackgroundColor3 = color3ToTable(obj.BackgroundColor3),
  120.             Position = udim2ToTable(obj.Position),
  121.             Size = udim2ToTable(obj.Size),
  122.             BackgroundTransparency = obj.BackgroundTransparency,
  123.             ImageTransparency = obj.ImageTransparency,
  124.             BorderSizePixel = obj.BorderSizePixel,
  125.             BorderColor3 = color3ToTable(obj.BorderColor3),
  126.             ScaleType = obj.ScaleType.Name,
  127.             Visible = obj.Visible,
  128.             Children = {}
  129.         }
  130.     end
  131.     return nil
  132. end
  133.  
  134. local function serializeSurfaceGui(gui)
  135.     local guiData = {
  136.         ClassName = "SurfaceGui",
  137.         Name = gui.Name,
  138.         Face = gui.Face.Name,
  139.         Enabled = gui.Enabled,
  140.         SizingMode = gui.SizingMode.Name,
  141.         PixelsPerStud = gui.PixelsPerStud,
  142.         Children = {}
  143.     }
  144.  
  145.     for _, child in ipairs(gui:GetChildren()) do
  146.         local childData = serializeGuiObject(child)
  147.         if childData then
  148.             table.insert(guiData.Children, childData)
  149.         end
  150.     end
  151.     return guiData
  152. end
  153.  
  154. local function serializeChildInstance(child)
  155.     if child:IsA("Decal") then
  156.         return {
  157.             ClassName = "Decal",
  158.             Name = child.Name,
  159.             Texture = child.Texture,
  160.             Transparency = child.Transparency,
  161.             Face = child.Face.Name
  162.         }
  163.     elseif child:IsA("PointLight") then
  164.         return {
  165.             ClassName = "PointLight",
  166.             Name = child.Name,
  167.             Brightness = child.Brightness,
  168.             Range = child.Range,
  169.             Color = color3ToTable(child.Color),
  170.             Shadows = child.Shadows,
  171.             Enabled = child.Enabled
  172.         }
  173.     elseif child:IsA("Fire") then
  174.         return {
  175.             ClassName = "Fire",
  176.             Name = child.Name,
  177.             Heat = child.Heat,
  178.             Size = child.Size,
  179.             Color = color3ToTable(child.Color),
  180.             SecondaryColor = color3ToTable(child.SecondaryColor),
  181.             Enabled = child.Enabled
  182.         }
  183.     elseif child:IsA("Smoke") then
  184.         return {
  185.             ClassName = "Smoke",
  186.             Name = child.Name,
  187.             Size = child.Size,
  188.             Opacity = child.Opacity,
  189.             Color = color3ToTable(child.Color),
  190.             Enabled = child.Enabled
  191.         }
  192.     elseif child:IsA("SurfaceGui") then
  193.         return serializeSurfaceGui(child)
  194.     elseif child:IsA("Texture") then
  195.         return {
  196.             ClassName = "Texture",
  197.             Name = child.Name,
  198.             Texture = child.Texture,
  199.             Transparency = child.Transparency,
  200.             Face = child.Face.Name,
  201.             StudsPerTileU = child.StudsPerTileU,
  202.             StudsPerTileV = child.StudsPerTileV
  203.         }
  204.     end
  205.     return nil
  206. end
  207.  
  208. local function splitDataIntoChunks(data)
  209.     local chunks = {}
  210.     local chunkIndex = 1
  211.     local currentChunk = {}
  212.     local currentSize = 0
  213.  
  214.     for i, part in ipairs(data) do
  215.         local partData = HttpService:JSONEncode(part)
  216.         local partSize = #partData
  217.  
  218.         if partSize > MAX_DATA_SIZE then
  219.             warn(`Part {i} ({part.Name}) is too large ({partSize} bytes) - skipped!`)
  220.             continue
  221.         end
  222.  
  223.         if currentSize + partSize > MAX_DATA_SIZE then
  224.             chunks[chunkIndex] = currentChunk
  225.             chunkIndex += 1
  226.             currentChunk = {}
  227.             currentSize = 0
  228.         end
  229.  
  230.         table.insert(currentChunk, part)
  231.         currentSize += partSize
  232.     end
  233.  
  234.     if #currentChunk > 0 then
  235.         chunks[chunkIndex] = currentChunk
  236.     end
  237.  
  238.     print(`Split data into {chunkIndex} chunks`)
  239.     return chunks, chunkIndex
  240. end
  241.  
  242. local function saveGlobalMap()
  243.  
  244.     -- Check if the game is running in a private or VIP server
  245.     if game.PrivateServerId ~= "" then
  246.         print("This is a private or VIP server. Skipping map saving.")
  247.         return
  248.     end
  249.  
  250.     -- Any other code you want to run when it's NOT a private/VIP server
  251.     print("This is not a private/VIP server. Continuing with the script...")
  252.     if isMapLoading then
  253.         warn("-- Map save requested while loading. Queued.")
  254.         table.insert(saveQueue, true)
  255.         return false
  256.     end
  257.  
  258.     print("-- Starting map save...")
  259.     local buildParts = {}
  260.  
  261.     for _, obj in ipairs(workspace:GetDescendants()) do
  262.         if obj:IsA("BasePart") and not obj:IsDescendantOf(Players) then
  263.             local partData = {
  264.                 ClassName = obj.ClassName,
  265.                 Name = obj.Name,
  266.                 Position = vector3ToTable(obj.Position),
  267.                 Size = vector3ToTable(obj.Size),
  268.                 Color = color3ToTable(obj.Color),
  269.                 Orientation = vector3ToTable(obj.Orientation),
  270.                 Anchored = obj.Anchored,
  271.                 Transparency = obj.Transparency,
  272.                 Material = obj.Material.Name,
  273.                 CanCollide = obj.CanCollide,
  274.                 Reflectance = obj.Reflectance,
  275.                 Locked = obj.Locked,
  276.                 TopSurface = obj.TopSurface.Name,
  277.                 BottomSurface = obj.BottomSurface.Name,
  278.                 LeftSurface = obj.LeftSurface.Name,
  279.                 RightSurface = obj.RightSurface.Name,
  280.                 FrontSurface = obj.FrontSurface.Name,
  281.                 BackSurface = obj.BackSurface.Name,
  282.                 Shape = obj:IsA("Part") and obj.Shape.Name or nil,
  283.                 Children = {},
  284.             }
  285.  
  286.             for _, child in ipairs(obj:GetChildren()) do
  287.                 local childData = serializeChildInstance(child)
  288.                 if childData then
  289.                     table.insert(partData.Children, childData)
  290.                 end
  291.             end
  292.  
  293.             table.insert(buildParts, partData)
  294.         end
  295.     end
  296.  
  297.     local chunks, totalChunks = splitDataIntoChunks(buildParts)
  298.     local mapMetadata = {
  299.         totalChunks = totalChunks,
  300.         lastSaveTime = os.time(),
  301.         version = 2
  302.     }
  303.  
  304.     -- Save metadata
  305.     local metadataStore = DataStoreService:GetDataStore("GlobalMapStore_Meta")
  306.     local success, err = pcall(function()
  307.         metadataStore:SetAsync("MapMetadata", mapMetadata)
  308.     end)
  309.  
  310.     if not success then
  311.         warn(`Metadata save failed: {err}`)
  312.         return false
  313.     end
  314.  
  315.     -- Save chunks
  316.     local chunksSaved = 0
  317.     local storeIndex = 1
  318.  
  319.     while storeIndex <= MAX_DATASTORES and chunksSaved < totalChunks do
  320.         local store = DataStoreService:GetDataStore("GlobalMapStore" .. storeIndex)
  321.         local maxThisStore = math.min(MAX_CHUNKS_PER_STORE, totalChunks - chunksSaved)
  322.  
  323.         for i = 1, maxThisStore do
  324.             local globalChunkIndex = chunksSaved + 1
  325.             local chunkKey = "GlobalMap_Chunk" .. globalChunkIndex
  326.  
  327.             local success, err = pcall(function()
  328.                 store:SetAsync(chunkKey, chunks[globalChunkIndex])
  329.             end)
  330.  
  331.             if success then
  332.                 print(`Saved chunk {globalChunkIndex} to store {storeIndex}`)
  333.                 chunksSaved += 1
  334.             else
  335.                 warn(`Failed to save chunk {globalChunkIndex}: {err}`)
  336.             end
  337.         end
  338.  
  339.         storeIndex += 1
  340.     end
  341.  
  342.     if chunksSaved == totalChunks then
  343.         print(`Map saved successfully across {storeIndex} stores with {totalChunks} chunks`)
  344.         return true
  345.     else
  346.         warn(`Failed to save all chunks. Saved {chunksSaved}/{totalChunks}`)
  347.         return false
  348.     end
  349. end
  350.  
  351. local function createGuiObjects(parent, children)
  352.     for _, childData in ipairs(children or {}) do
  353.         local childInstance = Instance.new(childData.ClassName)
  354.         childInstance.Name = childData.Name
  355.  
  356.         if childInstance:IsA("TextLabel") then
  357.             childInstance.Text = childData.Text
  358.             childInstance.TextColor3 = tableToColor3(childData.TextColor3)
  359.             childInstance.BackgroundColor3 = tableToColor3(childData.BackgroundColor3)
  360.             childInstance.TextSize = childData.TextSize
  361.             childInstance.Font = Enum.Font[childData.Font]
  362.             childInstance.Position = tableToUDim2(childData.Position)
  363.             childInstance.Size = tableToUDim2(childData.Size)
  364.             childInstance.BackgroundTransparency = childData.BackgroundTransparency
  365.             childInstance.TextTransparency = childData.TextTransparency
  366.             childInstance.TextXAlignment = Enum.TextXAlignment[childData.TextXAlignment]
  367.             childInstance.TextYAlignment = Enum.TextYAlignment[childData.TextYAlignment]
  368.             childInstance.TextWrapped = childData.TextWrapped
  369.             childInstance.TextScaled = childData.TextScaled
  370.             childInstance.BorderSizePixel = childData.BorderSizePixel
  371.             childInstance.BorderColor3 = tableToColor3(childData.BorderColor3)
  372.             childInstance.Visible = childData.Visible
  373.         elseif childInstance:IsA("Frame") then
  374.             childInstance.BackgroundColor3 = tableToColor3(childData.BackgroundColor3)
  375.             childInstance.Position = tableToUDim2(childData.Position)
  376.             childInstance.Size = tableToUDim2(childData.Size)
  377.             childInstance.BackgroundTransparency = childData.BackgroundTransparency
  378.             childInstance.BorderSizePixel = childData.BorderSizePixel
  379.             childInstance.BorderColor3 = tableToColor3(childData.BorderColor3)
  380.             childInstance.Visible = childData.Visible
  381.             createGuiObjects(childInstance, childData.Children)
  382.         elseif childInstance:IsA("ImageLabel") then
  383.             childInstance.Image = childData.Image
  384.             childInstance.ImageColor3 = tableToColor3(childData.ImageColor3)
  385.             childInstance.BackgroundColor3 = tableToColor3(childData.BackgroundColor3)
  386.             childInstance.Position = tableToUDim2(childData.Position)
  387.             childInstance.Size = tableToUDim2(childData.Size)
  388.             childInstance.BackgroundTransparency = childData.BackgroundTransparency
  389.             childInstance.ImageTransparency = childData.ImageTransparency
  390.             childInstance.BorderSizePixel = childData.BorderSizePixel
  391.             childInstance.BorderColor3 = tableToColor3(childData.BorderColor3)
  392.             childInstance.ScaleType = Enum.ScaleType[childData.ScaleType]
  393.             childInstance.Visible = childData.Visible
  394.         end
  395.  
  396.         childInstance.Parent = parent
  397.     end
  398. end
  399.  
  400. local function loadGlobalMap()
  401.     print("-- Loading map...")
  402.     isMapLoading = true
  403.     mapFullyLoaded = false
  404.     saveQueue = {}
  405.  
  406.     local metadataStore = DataStoreService:GetDataStore("GlobalMapStore_Meta")
  407.     local success, mapMetadata = pcall(function()
  408.         return metadataStore:GetAsync("MapMetadata")
  409.     end)
  410.  
  411.     if not success or not mapMetadata then
  412.         warn("-- No metadata found. Trying legacy load...")
  413.         local legacyStore = DataStoreService:GetDataStore("GlobalMapStore")
  414.         local success, data = pcall(function()
  415.             return legacyStore:GetAsync("GlobalMap_All")
  416.         end)
  417.  
  418.         if success and data then
  419.             print("-- Legacy map loaded")
  420.             createPartsFromData(data)
  421.             isMapLoading = false
  422.             mapFullyLoaded = true
  423.             saveGlobalMap()
  424.             return true
  425.         else
  426.             warn("-- No map data found. Creating new map...")
  427.             isMapLoading = false
  428.             mapFullyLoaded = true
  429.             saveGlobalMap()
  430.             return false
  431.         end
  432.     end
  433.  
  434.     print(`-- Loading {mapMetadata.totalChunks} chunks from metadata v{mapMetadata.version}`)
  435.  
  436.     local allParts = {}
  437.     local chunksLoaded = 0
  438.     local storeIndex = 1
  439.  
  440.     while storeIndex <= MAX_DATASTORES and chunksLoaded < mapMetadata.totalChunks do
  441.         local store = DataStoreService:GetDataStore("GlobalMapStore" .. storeIndex)
  442.         local maxThisStore = math.min(MAX_CHUNKS_PER_STORE, mapMetadata.totalChunks - chunksLoaded)
  443.  
  444.         for i = 1, maxThisStore do
  445.             local globalChunkIndex = chunksLoaded + 1
  446.             local chunkKey = "GlobalMap_Chunk" .. globalChunkIndex
  447.  
  448.             local success, chunkData = pcall(function()
  449.                 return store:GetAsync(chunkKey)
  450.             end)
  451.  
  452.             if success and chunkData then
  453.                 print(`Loaded chunk {globalChunkIndex} from store {storeIndex}`)
  454.                 for _, part in ipairs(chunkData) do
  455.                     table.insert(allParts, part)
  456.                 end
  457.                 chunksLoaded += 1
  458.             else
  459.                 warn(`Failed to load chunk {globalChunkIndex} from store {storeIndex}`)
  460.             end
  461.         end
  462.  
  463.         storeIndex += 1
  464.     end
  465.  
  466.     if chunksLoaded == mapMetadata.totalChunks then
  467.         print(`All {chunksLoaded} chunks loaded successfully`)
  468.         createPartsFromData(allParts)
  469.         task.delay(2, verifyPartAnchoring)
  470.         isMapLoading = false
  471.         mapFullyLoaded = true
  472.  
  473.         if #saveQueue > 0 then
  474.             print(`Processing {#saveQueue} queued saves`)
  475.             saveGlobalMap()
  476.             saveQueue = {}
  477.         end
  478.         return true
  479.     else
  480.         warn(`Loaded {chunksLoaded}/{mapMetadata.totalChunks} chunks`)
  481.         isMapLoading = false
  482.         return false
  483.     end
  484. end
  485.  
  486. function createPartsFromData(partData)
  487.     print(`-- Creating {#partData} parts...`)
  488.     local createdParts = {}
  489.     local failed = 0
  490.  
  491.     for i, part in ipairs(partData) do
  492.         local success, err = pcall(function()
  493.             local newPart = Instance.new(part.ClassName)
  494.             newPart.Name = part.Name
  495.             newPart:SetAttribute("ShouldBeAnchored", part.Anchored)
  496.  
  497.             newPart.Position = tableToVector3(part.Position)
  498.             newPart.Size = tableToVector3(part.Size)
  499.             newPart.Color = tableToColor3(part.Color)
  500.             newPart.Orientation = tableToVector3(part.Orientation)
  501.             newPart.Transparency = part.Transparency
  502.             newPart.Material = Enum.Material[part.Material]
  503.             newPart.CanCollide = part.CanCollide
  504.             newPart.Reflectance = part.Reflectance
  505.             newPart.Locked = part.Locked
  506.  
  507.             pcall(function()
  508.                 newPart.TopSurface = Enum.SurfaceType[part.TopSurface]
  509.                 newPart.BottomSurface = Enum.SurfaceType[part.BottomSurface]
  510.                 newPart.LeftSurface = Enum.SurfaceType[part.LeftSurface]
  511.                 newPart.RightSurface = Enum.SurfaceType[part.RightSurface]
  512.                 newPart.FrontSurface = Enum.SurfaceType[part.FrontSurface]
  513.                 newPart.BackSurface = Enum.SurfaceType[part.BackSurface]
  514.             end)
  515.  
  516.             if part.Shape and newPart:IsA("Part") then
  517.                 pcall(function()
  518.                     newPart.Shape = Enum.PartType[part.Shape]
  519.                 end)
  520.             end
  521.  
  522.             newPart.Anchored = part.Anchored
  523.             newPart.Parent = workspace
  524.             table.insert(createdParts, newPart)
  525.  
  526.             -- Handle non-GUI children
  527.             local hasGui = false
  528.             for _, childData in ipairs(part.Children or {}) do
  529.                 if childData.ClassName == "SurfaceGui" then
  530.                     hasGui = true
  531.                 else
  532.                     pcall(function()
  533.                         local child = Instance.new(childData.ClassName)
  534.                         child.Name = childData.Name
  535.  
  536.                         if child:IsA("Decal") then
  537.                             child.Texture = childData.Texture
  538.                             child.Transparency = childData.Transparency
  539.                             child.Face = Enum.NormalId[childData.Face]
  540.                         elseif child:IsA("Fire") then
  541.                             child.Heat = childData.Heat
  542.                             child.Size = childData.Size
  543.                             child.Color = tableToColor3(childData.Color)
  544.                             child.SecondaryColor = tableToColor3(childData.SecondaryColor)
  545.                             child.Enabled = childData.Enabled
  546.                         elseif child:IsA("Smoke") then
  547.                             child.Size = childData.Size
  548.                             child.Opacity = childData.Opacity
  549.                             child.Color = tableToColor3(childData.Color)
  550.                             child.Enabled = childData.Enabled
  551.                         elseif child:IsA("PointLight") then
  552.                             child.Brightness = childData.Brightness
  553.                             child.Range = childData.Range
  554.                             child.Color = tableToColor3(childData.Color)
  555.                             child.Shadows = childData.Shadows
  556.                             child.Enabled = childData.Enabled
  557.                         elseif child:IsA("Texture") then
  558.                             child.Texture = childData.Texture
  559.                             child.Transparency = childData.Transparency
  560.                             child.Face = Enum.NormalId[childData.Face]
  561.                             child.StudsPerTileU = childData.StudsPerTileU
  562.                             child.StudsPerTileV = childData.StudsPerTileV
  563.                         end
  564.  
  565.                         child.Parent = newPart
  566.                     end)
  567.                 end
  568.             end
  569.  
  570.             -- Handle GUI children
  571.             if hasGui then
  572.                 for _, childData in ipairs(part.Children or {}) do
  573.                     if childData.ClassName == "SurfaceGui" then
  574.                         pcall(function()
  575.                             local gui = Instance.new("SurfaceGui")
  576.                             gui.Name = childData.Name
  577.                             gui.Face = Enum.NormalId[childData.Face]
  578.                             gui.Enabled = childData.Enabled
  579.                             gui.SizingMode = Enum.SurfaceGuiSizingMode[childData.SizingMode]
  580.                             gui.PixelsPerStud = childData.PixelsPerStud
  581.                             gui.Parent = newPart
  582.                             createGuiObjects(gui, childData.Children)
  583.                         end)
  584.                     end
  585.                 end
  586.             end
  587.  
  588.             if i % 50 == 0 then
  589.                 task.wait()
  590.             end
  591.         end)
  592.  
  593.         if not success then
  594.             failed += 1
  595.             warn(`Failed part {i}: {err}`)
  596.         end
  597.     end
  598.  
  599.     print(`Created {#partData - failed}/{#partData} parts`)
  600.  
  601.     task.spawn(function()
  602.         task.wait(1)
  603.         for _, part in ipairs(workspace:GetDescendants()) do
  604.             if part:IsA("BasePart") and not part:IsDescendantOf(Players) then
  605.                 part.CastShadow = false
  606.  
  607.                 if part.Anchored == false and part:GetAttribute("ShouldBeAnchored") == true then
  608.                     for _, createdPart in ipairs(createdParts) do
  609.                         if createdPart == part then
  610.                             part.Anchored = true
  611.                             break
  612.                         end
  613.                     end
  614.                 end
  615.             end
  616.         end
  617.     end)
  618. end
  619.  
  620. local function setupCommandSystem()
  621.     local Commands = {}
  622.  
  623.     Commands.savemap = function(player)
  624.         if player.UserId == game.CreatorId or player:GetRankInGroup(game.CreatorId) >= 255 then
  625.             print(`Map save requested by {player.Name}`)
  626.             local success = saveGlobalMap()
  627.  
  628.             pcall(function()
  629.                 player.PlayerGui:SetCore("ChatMakeSystemMessage", {
  630.                     Text = success and "Map saved!" or "Save failed",
  631.                     Color = success and Color3.new(0, 1, 0) or Color3.new(1, 0, 0),
  632.                     Font = Enum.Font.SourceSansBold
  633.                 })
  634.             end)
  635.             return true
  636.         else
  637.             pcall(function()
  638.                 player.PlayerGui:SetCore("ChatMakeSystemMessage", {
  639.                     Text = "No permission",
  640.                     Color = Color3.new(1, 0, 0),
  641.                     Font = Enum.Font.SourceSansBold
  642.                 })
  643.             end)
  644.             return false
  645.         end
  646.     end
  647.  
  648.     local function onPlayerChatted(player, message)
  649.         local split = string.split(message, " ")
  650.         local command = string.lower(split[1])
  651.  
  652.         if string.sub(command, 1, 1) == "/" then
  653.             command = string.sub(command, 2)
  654.             if Commands[command] then
  655.                 Commands[command](player, split)
  656.             end
  657.         end
  658.     end
  659.  
  660.     local success, textChatService = pcall(function()
  661.         return game:GetService("TextChatService")
  662.     end)
  663.  
  664.     if success and textChatService then
  665.         textChatService.MessageReceived:Connect(function(msg)
  666.             local player = Players:FindFirstChild(msg.TextSource.Name)
  667.             if player then
  668.                 onPlayerChatted(player, msg.Text)
  669.             end
  670.         end)
  671.     else
  672.         local chatEvents = ReplicatedStorage:FindFirstChild("DefaultChatSystemChatEvents")
  673.         if chatEvents and chatEvents:FindFirstChild("OnMessageDoneFiltering") then
  674.             chatEvents.OnMessageDoneFiltering.OnClientEvent:Connect(function(data)
  675.                 local player = Players:FindFirstChild(data.FromSpeaker)
  676.                 if player then
  677.                     onPlayerChatted(player, data.Message)
  678.                 end
  679.             end)
  680.         else
  681.             Players.PlayerAdded:Connect(function(player)
  682.                 pcall(function()
  683.                     player.Chatted:Connect(function(msg)
  684.                         onPlayerChatted(player, msg)
  685.                     end)
  686.                 end)
  687.             end)
  688.  
  689.             for _, player in ipairs(Players:GetPlayers()) do
  690.                 pcall(function()
  691.                     player.Chatted:Connect(function(msg)
  692.                         onPlayerChatted(player, msg)
  693.                     end)
  694.                 end)
  695.             end
  696.         end
  697.     end
  698. end
  699.  
  700. game:BindToClose(function()
  701.     print("Saving on shutdown...")
  702.     if not isMapLoading and mapFullyLoaded then
  703.         saveGlobalMap()
  704.     else
  705.         warn("Skipping save: Map not fully loaded")
  706.     end
  707. end)
  708.  
  709. local function setupAutoSave()
  710.     local AUTO_SAVE_INTERVAL = 900 -- 15 minutes
  711.  
  712.     while true do
  713.         task.wait(AUTO_SAVE_INTERVAL)
  714.  
  715.         if not isMapLoading and mapFullyLoaded then
  716.             print("Auto-saving map...")
  717.             saveGlobalMap()
  718.         else
  719.             warn("Auto-save skipped: Map busy")
  720.             table.insert(saveQueue, true)
  721.         end
  722.     end
  723. end
  724.  
  725. local function initialize()
  726.     print("Initializing map system...")
  727.     task.spawn(function()
  728.         local success = loadGlobalMap()
  729.         print(success and "Map loaded!" or "Load failed")
  730.         remoteload:FireAllClients()
  731.     end)
  732.  
  733.     setupCommandSystem()
  734.     task.spawn(setupAutoSave)
  735.     print("Map system initialized!")
  736. end
  737.  
  738. Players.PlayerAdded:Connect(function(player)
  739.     player.Chatted:Connect(function(message)
  740.         if message == "/savemap" then
  741.             saveGlobalMap()
  742.         end
  743.     end)
  744. end)
  745.  
  746. Players.PlayerAdded:Connect(function(plr)
  747.     if isMapLoading == false then
  748.         remoteload:FireClient(plr)
  749.     end
  750. end)
  751.  
  752.  
  753.  
  754. initialize()
Advertisement
Add Comment
Please, Sign In to add comment