I really didn’t want to post here since it just felt like an easy fix but I’m at my wits end. Any help is much appreciated.
local ServerStorage = game:GetService("ServerStorage")
local PlayerService = game:GetService("Players")
local Datastore = require(script.Datastore)
_G.inTestingMode = true
PlayerService.PlayerAdded:Connect(Datastore.Load)
PlayerService.PlayerRemoving:Connect(Datastore.Save)
game:BindToClose(Datastore.ForceSave)
local Data = {}
local PlayerService = game:GetService("Players")
local DatastoreService = game:GetService("DataStoreService")
local Datastore = DatastoreService:GetDataStore("Statistics")
function Data.Load(Player)
local Data
local leaderstats
local PlayerID = "Player_" .. Player.UserId
-- Load folder
for i, v in ipairs(script.Parent.PlayerData:GetChildren()) do
v:Clone().Parent = Player
end
-- Collect data
local Success, ErrorMessage = pcall(function()
Data = Datastore:GetAsync(PlayerID)
end)
-- Load data
if Success and Data then
for i, v in ipairs(Player:FindFirstChildWhichIsA("Folder"):GetChildren()) do
v.Value = Data[v.Name]
end
end
local ServerStorage = game:GetService("ServerStorage")
local PlayerStates = ServerStorage.PlayerStates
local SubjectState = require(script.Parent.SubjectState)
-- Load player states
local StatesTemplate = script.Parent.PlayerStatesTemplate:Clone()
StatesTemplate.Name = Player.Name
StatesTemplate.Parent = PlayerStates
-- State interaction
for i, v in ipairs(PlayerStates:GetChildren()) do
v.Changed:Connect(function(Value)
SubjectState.Activate(Player, v.Name, Value)
end)
end
end
function Data.Save(Player)
-- Variables
local PlayerID = "Player_" .. Player.UserId
local Data = {}
-- Collect data
for i, v in ipairs(Player:FindFirstChildWhichIsA("Folder"):GetChildren()) do
Data[v.Name] = v.Value
end
-- Save data
local Success, ErrorMessage = pcall(function()
Datastore:SetAsync(PlayerID, Data)
end)
if not Success then
warn("Error: Data not saved.")
else
print("Data saved successfully.")
end
end
function Data.ForceSave()
for i, Player in ipairs(PlayerService:GetPlayers()) do
task.spawn(function()
Data.Save(Player)
end)
end
end
return Data
Alright, so I can suggest you my DataStore solution checklist I have:
Make sure the Datastore service is enabled in your game settings.
Check if the PlayerData folder exists in the script.Parent and it has the necessary data.
Ensure the PlayerStates folder exists in ServerStorage.
Make sure the SubjectState module exists and has an Activate function.
Check if the PlayerStatesTemplate exists in script.Parent.
Ensure that the data you’re trying to save is serializable. Roblox’s DataStore service can only save basic Lua types (string, number, boolean, table, etc.).
Use print or warn to output ErrorMessage if the pcall fails. This can give you more information about what’s going wrong.
Most of these checks would break the entire script and would be highlighted so it would be an easy fix, however code above SetAsync only works and not below it.
Bingo. I was looking at a comment on a previous Datastore tutorial and that’s what it said. That being said, I did include a BindToClose function which was supposed to save data when the server shuts down before the player can save data on time. So it technicnally works, just not during BindToClose, which triggers during an event of a global server shutdown or when a server of 1 person leaves and the game shuts down while the player is leaving.
Did some tweaking and got a really slow server shutdown. I guess it will do for now until I can find a better fix for it. It appears to be saving on a loop or atleast saving more than once too quickly.