shouldn’t your datastore be in a pcall(), at least all the times i’ve done with datastores i’ve had the datastore in a pcall. I don’t know much about data stores but i think this could possibly be the problem. don’t quote me on that
for the getting the info:
local success,data = pcall(function()
return DS:GetAsync(Plr.UserId)
end)
for saving:
local success = pcall(function()
DS:SetAsync(Plr.UserId,save)
end)
local Players = game:GetService("Players")
local DataStores = game:GetService("DataStoreService")
local DataStore = DataStores:GetDataStore("DataStore")
local ProtectedCall = pcall
Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player
local Clicks = Instance.new("IntValue")
Clicks.Name = "Clicks"
Clicks.Parent = Leaderstats
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = Leaderstats
local Aura = Instance.new("StringValue")
Aura.Name = "Aura"
Aura.Parent = Leaderstats
local Success, Result = ProtectedCall(function()
return DataStore:GetAsync("Stats_"..Player.UserId)
end)
if Success then
if Result then
if type(Result) == "table" then
Clicks.Value = Result[1] or 0
Coins.Value = Result[2] or 0
Aura.Value = Result[3] or ""
end
end
end
end)
Players.PlayerRemoving:Connect(function(Player)
local Success, Result = ProtectedCall(function()
return DataStore:SetAsync("Stats_"..Player.UserId, {Player.leaderstats.Clicks.Value, Player.leaderstats.Coins.Value, Player.leaderstats.Aura.Value})
end)
if Success then
print(Result)
else
warn(Result)
end
end)
game:BindToClose(function()
for _, Player in ipairs(Players:GetPlayers()) do
local Success, Result = ProtectedCall(function()
return DataStore:SetAsync("Stats_"..Player.UserId, {Player.leaderstats.Clicks.Value, Player.leaderstats.Coins.Value, Player.leaderstats.Aura.Value})
end)
if Success then
print(Result)
else
warn(Result)
end
end
end)
Make sure no other script(s) is/are changing the name of any of the leaderstats as one of the previous replies pointed out.
pcall() isn’t required but it’s recommended because if the DataStore request fails then an error is thrown/raised, by wrapping the DataStore request inside a pcall (protected call), if an error is thrown/raised it is handled by the pcall, if the request was not wrapped inside a pcall when it failed then the execution of the entire script would break (abruptly stop) as the thrown/raised error wouldn’t be handled.
This isn’t necessary, when a “StringValue” instance is created its “Value” property points to nil unlike other similar instances such as “NumberValue”/“IntValue” which have their “Value” property assigned a value of 0 by default. Due to this the DataStore was saving a nil value (what represents a lack of a value in Lua) for the “StringValue” instance.
To remedy this you can just set some default value for the “Value” property of the “StringValue” instance, typically I go with an empty string value "", it’s still a string value but it’s a good way of representing nothing (like 0 for numbers/integers). By doing this, when the data is saved to the DataStore this default empty string value will be stored instead of a nil value.