local dt = game:GetService("DataStoreService")
local storage = dt:GetDataStore('PrivateServerSaves')
local doNotSave = false
function saveBlocks()
if game.VIPServerId ~= '' then
if doNotSave == false and game:GetService("ServerStorage"):WaitForChild("GameLoaded").Value == true then
local saved= {}
for _,i in pairs(game:GetService("Workspace"):GetChildren()) do
if i:IsA("SpawnLocation") and i.Name == 'gamespawn__' then
local spawnInfo = {}
local t = {
i.Position.X,
i.Position.Y,
i.Position.Z
}
local t2 = {
i.Size.X,
i.Size.Y,
i.Size.Z
}
table.insert(spawnInfo, '(spawn)')
table.insert(spawnInfo, t)
table.insert(spawnInfo, t2)
table.insert(saved, spawnInfo)
end
end
for _,i in pairs(game:GetService("Workspace"):WaitForChild("Blocks__"):GetChildren()) do
local blockInTable = {}
table.insert(blockInTable, i:WaitForChild("ModelName").Value)
table.insert(blockInTable, {i.Position.X, i.Position.Y, i.Position.Z})
table.insert(blockInTable, i:WaitForChild("Owner").Value)
table.insert(saved, blockInTable)
end
for _,i in pairs(game:GetService("Workspace"):WaitForChild("Entities__"):GetChildren()) do
local blockInTable = {}
if i:FindFirstChild("HumanoidRootPart") ~= nil and i:FindFirstChildWhichIsA("Humanoid") ~= nil then
table.insert(blockInTable, '(entity)')
table.insert(blockInTable, {i:WaitForChild("HumanoidRootPart").Position.X, i:WaitForChild("HumanoidRootPart").Position.Y, i:WaitForChild("HumanoidRootPart").Position.Z})
table.insert(blockInTable, string.sub(i.Name, #'_Entity_' + 1, #i.Name))
table.insert(blockInTable, i:FindFirstChildWhichIsA("Humanoid").Health)
table.insert(saved, blockInTable)
end
end
storage:SetAsync(tostring(game.VIPServerId), saved)
end
end
end
local blocksLoaded = 0
local maxLoadableBlocks = 30
if game.PrivateServerOwnerId == 0 and game.PrivateServerId ~= '' then
coroutine.wrap(function()
while wait(10) do
local listOfPlayersCool = {}
for _,i in pairs(game:GetService("Players"):GetPlayers()) do
table.insert(listOfPlayersCool, i.Name)
end
game:GetService("MessagingService"):PublishAsync(game.PrivateServerId, game:GetService("HttpService"):JSONEncode(
{
Players = #game.Players:GetPlayers(),
PlayerList = listOfPlayersCool,
Ping = 0,
Id = -1,
ServerName = ''
}))
end
end)()
end
if game.PrivateServerId ~= '' then
coroutine.wrap(function()
while wait(180) do
saveBlocks()
end
end)()
end
function loadBlocks()
if game.VIPServerId ~= '' then
local datastore = storage:GetAsync(tostring(game.VIPServerId))
local datastore = storage.GetAsync(storage, tostring(game.VIPServerId))
local work, data = pcall(storage.GetAsync, storage, tostring(game.VIPServerId))
if work == false then
for _,i in pairs(game:GetService("Players"):GetPlayers()) do
i:Kick('Server failed to load!')
end
return end
if work == true then
if data == nil or data == {} then
defaultMapLoad()
else
coroutine.wrap(function()
local loadedFully = false
for _,i in pairs(data) do
if i[1] ~= '(spawn)' and i[1] ~= '(entity)' then
if i[1] ~= 'WaterTest' then
game:GetService("ServerStorage"):WaitForChild("CurrentlyLoading").Value = 'Blocks'
if blocksLoaded > maxLoadableBlocks then
blocksLoaded = 0
wait()
end
blocksLoaded = blocksLoaded + 1
local pro = nil
if i[3] == 'game' then
pro = 'game'
end
placeModel('At', Vector3.new(i[2][1], i[2][2], i[2][3]), i[1], false, false, pro, i[3])
else
coroutine.wrap(function()
repeat wait() until loadedFully == true
game:GetService("ServerStorage"):WaitForChild("CurrentlyLoading").Value = 'Blocks'
if blocksLoaded > maxLoadableBlocks then
blocksLoaded = 0
wait()
end
blocksLoaded = blocksLoaded + 1
local pro = nil
if i[3] == 'game' then
pro = 'game'
end
placeModel('At', Vector3.new(i[2][1], i[2][2], i[2][3]), i[1], false, false, pro, i[3])
end)()
end
else
if i[1] == '(spawn)' then
game:GetService("ServerStorage"):WaitForChild("CurrentlyLoading").Value = 'Spawnpoint'
if blocksLoaded > maxLoadableBlocks then
blocksLoaded = 0
wait()
end
blocksLoaded = blocksLoaded + 1
local newSpawn = Instance.new("SpawnLocation", game:GetService("Workspace"))
newSpawn.Position = Vector3.new(i[2][1],i[2][2],i[2][3])
newSpawn.Size = Vector3.new(i[3][1],i[3][2],i[3][3])
newSpawn.Anchored = true
newSpawn.Name = 'gamespawn__'
newSpawn.CanCollide = false
newSpawn:ClearAllChildren()
newSpawn.CanTouch = true
newSpawn.Locked = false
newSpawn.Duration = 0
newSpawn.CanQuery = false
newSpawn.Transparency = 1
end
if i[1] == '(entity)' then
game:GetService("ServerStorage"):WaitForChild("CurrentlyLoading").Value = 'Entities'
if blocksLoaded > maxLoadableBlocks then
blocksLoaded = 0
wait()
end
blocksLoaded = blocksLoaded + 1
local entity = game:GetService("ServerStorage"):WaitForChild("Entities"):FindFirstChild(i[3])
if entity ~= nil then
entity = entity:Clone()
local root = entity:WaitForChild("HumanoidRootPart")
local pos = Vector3.new(i[2][1],i[2][2],i[2][3])
root.CFrame = CFrame.new(pos, pos + root.CFrame.LookVector)
entity:FindFirstChildWhichIsA("Humanoid").Health = i[4]
entity.Parent = game:GetService('Workspace'):WaitForChild("Entities__")
end
end
end
end
loadedFully = true
game:GetService("ServerStorage"):WaitForChild("GameLoaded").Value = true
if #game:GetService("Workspace"):WaitForChild("Blocks__"):GetChildren() <= 5 then
doNotSave = true
for _,i in pairs(game:GetService("Players"):GetPlayers()) do
i:Kick('Server failed to load!')
end
end
end)()
end
end
end
end
function loadServer()
if game.VIPServerId ~= '' then
loadBlocks()
else
defaultMapLoad()
end
end
game.Close:Connect(function()
saveBlocks()
end)
Heres the code for the server saving.