Hello, I am trying to make a Survival Game that will have servers that are open 24/7 and will wipe once every week, thinking about on a Friday.
I am thinking of having 3 servers that are open and will save whatever is inside of workspace, including models, gears, parts, models with scripts, etc, unless it has a humanoid.
I was thinking about doing reserved servers to mimic this where each server will have its own access code to differientiate between servers automatically, and each server will save their own data and will wipe simultaneously every friday or whenever I update the game.
I have tried to make a script, and the closest I have gotten is with this script that will save anything inside of a Folder known as ‘World’. However, I want to see if I can do this as a last resort because there are things that might not work well inside of a folder instead of workspace.
local DataStoreService = game:GetService(“DataStoreService”)
local HttpService = game:GetService(“HttpService”)
local worldDataStore = DataStoreService:GetDataStore(“WorldDataStore”)
local function saveInstance(instance)
local data = {
ClassName = instance.ClassName,
Name = instance.Name,
Properties = {}
}
-- Save properties based on instance type
if instance:IsA("BasePart") then
data.Properties.Position = instance.Position
data.Properties.Size = instance.Size
data.Properties.Color = instance.Color
data.Properties.Material = instance.Material
data.Properties.Transparency = instance.Transparency
elseif instance:IsA("Model") then
data.Properties.PrimaryPartCFrame = instance.PrimaryPart and instance.PrimaryPart.CFrame or nil
end
-- Recursively save children, skipping Script and LocalScript
data.Children = {}
for _, child in pairs(instance:GetChildren()) do
if not (child:IsA("Script") or child:IsA("LocalScript")) then
table.insert(data.Children, saveInstance(child))
end
end
return data
end
local function loadInstance(data, parent)
print("Loading instance: " … data.Name) – Debugging print
local instance = Instance.new(data.ClassName)
instance.Name = data.Name
-- Load properties based on instance type
if data.ClassName == "Part" then
instance.Position = data.Properties.Position
instance.Size = data.Properties.Size
instance.Color = data.Properties.Color
instance.Material = data.Properties.Material
instance.Transparency = data.Properties.Transparency
elseif data.ClassName == "Model" then
-- Will handle the primary part later after children are loaded
end
-- Recursively load children
for _, childData in pairs(data.Children) do
loadInstance(childData, instance)
end
instance.Parent = parent
-- Set PrimaryPart for Models after children have been loaded
if data.ClassName == "Model" then
if data.Properties.PrimaryPartCFrame then
print("PrimaryPartCFrame found for model:", data.Name) -- Debugging print
if instance.PrimaryPart then
print("Setting PrimaryPartCFrame for model:", data.Name) -- Debugging print
instance:SetPrimaryPartCFrame(data.Properties.PrimaryPartCFrame)
else
print("No PrimaryPart found for model:", data.Name) -- Debugging print
end
end
end
return instance
end
local function saveWorldData()
print(“Saving world data…”) – Debugging print
local worldFolder = workspace:FindFirstChild(“World”)
if not worldFolder then
warn(“World folder not found in Workspace.”)
return
end
local worldData = {}
for _, child in pairs(worldFolder:GetChildren()) do
table.insert(worldData, saveInstance(child))
end
-- Convert the worldData table to a JSON string
local jsonData = HttpService:JSONEncode(worldData)
print("JSON Data to save:", jsonData) -- Debugging print
local success, errorMessage = pcall(function()
print("Attempting to save data to DataStore...") -- Debugging print
worldDataStore:SetAsync("WorldData", jsonData)
end)
if success then
print("World data saved successfully.")
else
warn("Failed to save world data: " .. errorMessage)
end
end
local function loadWorldData()
print(“Loading world data…”) – Debugging print
local success, jsonData = pcall(function()
print(“Attempting to load data from DataStore…”) – Debugging print
return worldDataStore:GetAsync(“WorldData”)
end)
if success and jsonData then
print("Loaded JSON Data:", jsonData) -- Debugging print
-- Convert the JSON string back to a table
local worldData = HttpService:JSONDecode(jsonData)
local worldFolder = workspace:FindFirstChild("World")
if not worldFolder then
worldFolder = Instance.new("Folder")
worldFolder.Name = "World"
worldFolder.Parent = workspace
end
-- Clear existing children
worldFolder:ClearAllChildren()
print("Cleared existing children in World folder.") -- Debugging print
for _, data in pairs(worldData) do
loadInstance(data, worldFolder)
end
print("World data loaded successfully.")
else
warn("Failed to load world data: " .. tostring(jsonData))
end
end
local function clearWorldData()
print(“Clearing world data…”) – Debugging print
local success, errorMessage = pcall(function()
worldDataStore:RemoveAsync(“WorldData”)
end)
if success then
print("World data cleared successfully.")
else
warn("Failed to clear world data: " .. errorMessage)
end
end
– Load data when the game starts
print(“Calling loadWorldData at game start…”) – Debugging print
loadWorldData()
– Save data periodically (e.g., every 5 minutes) in a separate coroutine
coroutine.wrap(function()
while true do
print(“Entering save loop…”) – Debugging print
saveWorldData()
wait(300) – 5 minutes
end
end)()
– Manually call this function to clear the saved data
– clearWorldData()
This script appears to save somewhat, giving me the information for whatever is in the folder, but it doesn’t load anything and just wipes whatever is in the folder. Does anyone know what is wrong with this script?
Thank you.