I am in the process of creating a datastore system for my game, but I keep running into the error pictured below:
This is very frusturating, as I have to wait 30 seconds every time I click the stop button in my studio session. I’ve looked into many posts regarding this topic but I haven’t been able to find a solution.
Just for some context, I am trying to store information about randomly generated NPC models that are unique to the player.
Here is the code:
-- Services
local dss = game:GetService("DataStoreService")
local playerDataStore = dss:GetDataStore("PlayerData")
local plrs = game:GetService("Players")
local wrkspce = game:GetService("Workspace")
local cs = game:GetService("CollectionService")
local runservice = game:GetService("RunService")
-- Functions
local function saveData(player: Player)
local key = ("Player: " ..tostring(player.UserId))
local data = {}
for _, item in pairs(player:WaitForChild("PlayerNPCFolder"):GetChildren()) do
if item:IsA("Model") then
local bodycolors = {}
local gender
for _, bodypart in pairs(item:GetChildren()) do
if bodypart.Name == "Head" or bodypart:IsA("MeshPart") then
local colors = {
math.floor(bodypart.Color.R*255),
math.floor(bodypart.Color.G*255),
math.floor(bodypart.Color.B*255)}
table.insert(bodycolors, colors)
end
end
for _, tag in pairs(cs:GetTags(item)) do
if tag == "Male" or tag == "Female" then
gender = tag
end
end
table.insert(data, {
item.Name,
item:FindFirstChildWhichIsA("Accessory"),
bodycolors,
gender
})
end
end
local success, err
repeat
success, err = pcall(function()
playerDataStore:UpdateAsync(key, function()
return data
end)
end)
task.wait()
until success
if not success then
warn("Failed to save "..player.Name.."'s data! (" ..tostring(player.UserId).. ")")
end
print(data)
end
local function loadData(player: Player)
local key = ("Player: " ..tostring(player.UserId))
local success, err
local data
-- Game Folder
local workspaceFolder = Instance.new("Folder")
workspaceFolder.Parent = wrkspce.CurrentNPCs
workspaceFolder.Name = player.Name
-- Player Folders
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local plrNPCs = Instance.new("Folder")
plrNPCs.Parent = player
plrNPCs.Name = "PlayerNPCFolder"
local deceasedPlrNPCs = Instance.new("Folder")
deceasedPlrNPCs.Parent = player:WaitForChild("PlayerNPCFolder")
deceasedPlrNPCs.Name = (player.Name.. "'s Deceased NPCs")
repeat
success, err = pcall(function()
data = playerDataStore:GetAsync(key)
end)
until success or not plrs:FindFirstChild(player.Name)
if success then
for _, items in pairs(data) do
if nil then
print(player.Name.. " has no data")
else
-- stuff for later
end
print(data)
end
end
end
-- Events
plrs.PlayerAdded:Connect(loadData)
plrs.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
for _, plr: Player in pairs(plrs:GetPlayers()) do
saveData(plr)
end
end)
This is my first time working with datastores, so if there is anything I could be doing better or more efficiently, feel free to let me know. Thanks!