I normally don’t use datastores, as I am new to them.
How can I fix my table saving code?
--// Services
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
--// Variables
local DS = DSS:GetOrderedDataStore("Cash")
local DS2 = DSS:GetOrderedDataStore("Maps")
--// Function
local function ConvertToTable(workspace2)
local Things = workspace2:GetChildren()
local Table = {}
for i, obj in pairs(Things) do
if obj:IsA("BasePart") then
print("Part Saved (" .. i .. "/" .. #Things .. ")")
Table[i] = {["Platform"] = workspace2.Parent.Platform.Position - obj.Position, ["Orient"] = obj.Orientation}
end
end
return Table
end
local function OnPlayerAdded(player:Player)
local workspace = player.PlayerGui:WaitForChild("Plot")
repeat task.wait() until workspace.Value ~= nil
workspace = workspace.Value:WaitForChild("Blocks")
local D = false
local plr_key = "id_"..player.UserId
local success, data = pcall(function()
return DS:GetAsync(plr_key)
end)
local success2, data2 = pcall(function()
return DS2:GetAsync(plr_key)
end)
if data2 ~= nil then
print("Loading Parts: " .. #data2)
for i, obj in pairs(data2) do
print("Part Loaded (" .. i .. "/" .. #data2 .. ")")
local Obj = game.ServerStorage.Objects.Wall:Clone()
Obj.Position = workspace.Platform.Position + obj[1]
Obj.Orientation = obj[2]
end
end
local stats = Instance.new("Folder")
stats.Parent = player
local cash = Instance.new("IntValue")
cash.Parent = stats
cash.Name = "Ichor"
if success then
cash.Value = data or 1000
end
game["Run Service"].Heartbeat:Connect(function()
if D == false then
D = true
cash.Value += math.random(10, 30)
if cash.Value > 100000 then
cash.Value = 100000
end
wait(15)
D = false
end
end)
end
local function OnPlayerRemoving(player)
local workspace = game.ServerStorage:WaitForChild("Plot")
workspace2 = workspace.Value:FindFirstChild("Blocks")
workspace:Destroy()
if workspace2 then
local plr_key = "id_"..player.UserId
local success, result = pcall(function()
local TNBLR = ConvertToTable(workspace2)
return DS:SetAsync(plr_key, player.Folder.Ichor.Value),DS2:SetAsync(plr_key, TNBLR)
end)
if not success then
warn(result)
end
end
end
local function OnServerShutdown()
for _, player in Players:GetPlayers() do
local workspace = game.ServerStorage:WaitForChild("Plot")
workspace2 = workspace.Value:FindFirstChild("Blocks")
workspace:Destroy()
if workspace2 then
local plr_key = "id_"..player.UserId
local success, result = pcall(function()
local TNBLR = ConvertToTable(workspace2)
return DS:SetAsync(plr_key, player.Folder.Ichor.Value),DS2:SetAsync(plr_key, TNBLR)
end)
if not success then
warn(result)
end
end
end
end
--// Connections
Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
game:BindToClose(OnServerShutdown)
Go to View and click on Output in Roblox Studio, to check if there are any errors. Since you are using OrderedDatastores, instead of a normal DataStore, you can only store positive integers.
Swapping to a regular datastore, by changing :GetOrderedDataStore() to :GetDataStore(), this should allow for saving the data you are looking to save and load. Any current data, will not be present upon swapping to the new Datastore.
When it comes ordered data store it needs pages and has a different syntax. For example instead of :GetAsync I believe you have to use :GetSortedAsync.
Is the object in the onPlayerAdded function meant to be parented to Workspace as in your code you set the position but no parent.
if data2 ~= nil then
print("Loading Parts: " .. #data2)
for i, obj in pairs(data2) do
print("Part Loaded (" .. i .. "/" .. #data2 .. ")")
local Obj = game.ServerStorage.Objects.Wall:Clone()
Obj.Position = workspace.Platform.Position + obj[1]
Obj.Orientation = obj[2]
Obj.Parent = workspace --set any parent.
end
end
Could this be why you believe the Datastore is not working or is there another reason?
edited the code, now it’s the positions not saving
--// Services
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
--// Variables
local DS = DSS:GetDataStore("Cash2")
local DS2 = DSS:GetDataStore("Maps5")
--// Function
local function ConvertToTable(workspace2)
local Things = workspace2:GetChildren()
local Table = {}
for i, obj in pairs(Things) do
if obj:IsA("BasePart") then
print("Part Saved (" .. i .. "/" .. #Things .. ")")
Table[i] = {[1] = (workspace2.Parent.Platform.Position - obj.Position), [2] = obj.Orientation}
end
end
return game.HttpService:JSONEncode(Table)
end
local function OnPlayerAdded(player:Player)
local workspace = player.PlayerGui:WaitForChild("Plot")
repeat task.wait() until workspace.Value ~= nil
workspace = workspace.Value:WaitForChild("Blocks")
local D = false
local plr_key = "id_"..player.UserId
local success, data = pcall(function()
return DS:GetAsync(plr_key)
end)
local success2, data2 = pcall(function()
return DS2:GetAsync(plr_key)
end)
if data2 ~= nil then
print("Loading Parts: " .. #data2)
for i, obj in pairs(game.HttpService:JSONDecode(data2)) do
print("Part Loaded (" .. i .. "/" .. #data2 .. ")")
local Obj = game.ServerStorage.Objects.Wall:Clone()
Obj.Position = workspace.Parent.Platform.Position + obj[1]
Obj.Orientation = obj[2]
Obj.Parent = workspace
end
end
local stats = Instance.new("Folder")
stats.Parent = player
local cash = Instance.new("IntValue")
cash.Parent = stats
cash.Name = "Ichor"
if success then
cash.Value = data or 1000
end
game["Run Service"].Heartbeat:Connect(function()
if D == false then
D = true
cash.Value += math.random(10, 30)
if cash.Value > 100000 then
cash.Value = 100000
end
wait(15)
D = false
end
end)
end
local function OnPlayerRemoving(player)
local workspace = game.ServerStorage:WaitForChild("Plot")
workspace2 = workspace.Value:FindFirstChild("Blocks")
workspace:Destroy()
if workspace2 then
local plr_key = "id_"..player.UserId
local success, result = pcall(function()
local TNBLR = ConvertToTable(workspace2)
return DS:SetAsync(plr_key, 0),DS2:SetAsync(plr_key, TNBLR)
end)
if not success then
warn(result)
end
end
end
local function OnServerShutdown()
for _, player in Players:GetPlayers() do
local workspace = game.ServerStorage:WaitForChild("Plot")
workspace2 = workspace.Value:FindFirstChild("Blocks")
workspace:Destroy()
if workspace2 then
local plr_key = "id_"..player.UserId
local success, result = pcall(function()
local TNBLR = ConvertToTable(workspace2)
return DS:SetAsync(plr_key, 0),DS2:SetAsync(plr_key, TNBLR)
end)
if not success then
warn(result)
end
end
end
end
--// Connections
Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
game:BindToClose(OnServerShutdown)