So I am saving a lot of inventory values in players and I have noticed some of the data (I save each catagory individually, in this case it was hair that was nil) is returning nil in studio and sometimes ingame.
Is this an issue with my data saving or a roblox issue? Should I be worried around it?Please show us your script. Did you wrap all this in a pcall?
All the datasaving in each catagory looks somewhat like this: And then in the script is the folder, To know if a player owns an item the value is set to 1
local PSDataStore = game:GetService("DataStoreService"):GetDataStore("APBodyAcc")
local Stats = script.BodyAcc
local StatsHolderName = "BodyAcc"
local StatsHolderType = "Folder"
game.Players.PlayerAdded:connect(function(NewPlayer)
local StatsHolder = Instance.new(StatsHolderType, NewPlayer)
StatsHolder.Name = StatsHolderName
local Key = "BodyAcc-" .. NewPlayer.Name
local GetSave = PSDataStore:GetAsync(Key)
for _, Stat in pairs(Stats:GetChildren()) do
local NewStats = Instance.new(Stat.ClassName, StatsHolder)
NewStats.Name = Stat.Name
NewStats.Value = Stat.Value
end
if GetSave then --Load Data
for _n, ls in pairs(StatsHolder:GetChildren()) do
ls.Value = GetSave[_n]
end
else --Saves data!
local StatsToSave = {}
for _n, ss in pairs(StatsHolder:GetChildren()) do
table.insert(StatsToSave, ss.Value)
end
warn("Created New DataStore!")
PSDataStore:SetAsync(Key, StatsToSave)
end
while wait(300) do
local StatsToSave = {}
for _n, ss in pairs(StatsHolder:GetChildren()) do
table.insert(StatsToSave, ss.Value)
end
warn("Stats saved!")
PSDataStore:SetAsync(Key, StatsToSave)
end
end)
game.Players.PlayerRemoving:connect(function(OldPlayer)
local Key = "BodyAcc-" .. OldPlayer.Name
local GetSave = PSDataStore:GetAsync(Key)
local StatsToSave = {}
for _n, ss in pairs(OldPlayer[StatsHolderName]:GetChildren()) do
table.insert(StatsToSave, ss.Value)
end
warn("Stats saved!")
PSDataStore:SetAsync(Key, StatsToSave)
end)
These scripts are the ones that are returning nil most of the time
while true do
wait(1)
local itemname = script.Parent.Name
local catagory = script.Parent.Catagory.Value
local findstat = game.Players.LocalPlayer:FindFirstChild(catagory)
local player = findstat:FindFirstChild(itemname)
if player.Value >= 1 then
script.Parent.Visible = true
else script.Parent.Visible = false
end
end
You didn’t wrap these into a pcall. A pcall is used to ensure that the data is saved. Try the following in both PlayerAdded and PlayerRemoving:
PlayerAdded:
local GetSave
local success, response = pcall(function()
GetSave = DataStore:GetAsync(Key)
end)
PlayerRemoving:
local GetSave
local success, response = pcall(function()
GetSave = DataStore:GetAsync(Key, StatsToSave)
end)
Im not the best scripter, have I done it correctly? Thank you so much for the help
local PSDataStore = game:GetService("DataStoreService"):GetDataStore("APBodyAcc")
local Stats = script.BodyAcc
local StatsHolderName = "BodyAcc"
local StatsHolderType = "Folder"
game.Players.PlayerAdded:connect(function(NewPlayer)
local StatsHolder = Instance.new(StatsHolderType, NewPlayer)
StatsHolder.Name = StatsHolderName
local Key = "BodyAcc-" .. NewPlayer.Name
local GetSave = PSDataStore:GetAsync(Key)
local GetSave
local success, response = pcall(function()
GetSave = PSDataStore:GetAsync(Key)
end)
for _, Stat in pairs(Stats:GetChildren()) do
local NewStats = Instance.new(Stat.ClassName, StatsHolder)
NewStats.Name = Stat.Name
NewStats.Value = Stat.Value
end
if GetSave then --Load Data
for _n, ls in pairs(StatsHolder:GetChildren()) do
ls.Value = GetSave[_n]
end
else --Saves data!
local StatsToSave = {}
for _n, ss in pairs(StatsHolder:GetChildren()) do
table.insert(StatsToSave, ss.Value)
end
warn("Created New DataStore!")
PSDataStore:SetAsync(Key, StatsToSave)
end
while wait(300) do
local StatsToSave = {}
for _n, ss in pairs(StatsHolder:GetChildren()) do
table.insert(StatsToSave, ss.Value)
end
warn("Stats saved!")
PSDataStore:SetAsync(Key, StatsToSave)
end
end)
game.Players.PlayerRemoving:connect(function(OldPlayer)
local Key = "BodyAcc-" .. OldPlayer.Name
local GetSave = PSDataStore:GetAsync(Key)
local StatsToSave = {}
for _n, ss in pairs(OldPlayer[StatsHolderName]:GetChildren()) do
table.insert(StatsToSave, ss.Value)
end
local GetSave
local success, response = pcall(function()
GetSave = PSDataStore:GetAsync(Key, StatsToSave)
end)
warn("Stats saved!")
PSDataStore:SetAsync(Key, StatsToSave)
end)
Please use a game:BindToClose function in combination with a coroutine to save all your data once the server shuts down in addition the the removing function, it often doesn’t have enough time to save everything before the server shuts down: