It is as it says, I need to know why my datastore is not saving data. I've tried many times to fix this but I am not very good with datastore so I need some help with this.
Note: It creates data for the player but then it does not save.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStore = DataStoreService:GetDataStore("Store")
--// Settings \\--
local StarterData = {
Coins = 0;
Knowledge = 0;
Multiplier = 1;
Capacity = 10;
BooksUnlocked = 1;
BookEquipped = 0;
BooksBought = 0;
}
local playersavetable = {}
local AUTO_SAVE_INTERVAL = 180;
--// Functions \\--
local function loadStarterData(Player)
local Stats = Instance.new("Folder")
Stats.Name = "leaderstats"
Stats.Parent = Player
local NotShowing = Instance.new("Folder")
NotShowing.Name = "NotShowing"
NotShowing.Parent = Stats
for statname, statvalue in pairs(StarterData) do
if statname == "Capacity" then
local intvalue = Instance.new("IntValue")
intvalue.Name = statname
intvalue.Value = statvalue
intvalue.Parent = NotShowing
elseif statname == "Multiplier" then
local intvalue = Instance.new("IntValue")
intvalue.Name = statname
intvalue.Value = statvalue
intvalue.Parent = NotShowing
elseif statname == "BooksUnlocked" then
local intvalue = Instance.new("IntValue")
intvalue.Name = statname
intvalue.Value = statvalue
intvalue.Parent = NotShowing
elseif statname == "BookEquipped" then
local intvalue = Instance.new("IntValue")
intvalue.Name = statname
intvalue.Value = statvalue
intvalue.Parent = NotShowing
elseif statname == "BooksBought" then
local intvalue = Instance.new("IntValue")
intvalue.Name = statname
intvalue.Value = statvalue
intvalue.Parent = NotShowing
elseif type(statvalue) == "number" then
local intvalue = Instance.new("IntValue")
intvalue.Name = statname
intvalue.Value = statvalue
intvalue.Parent = Stats
elseif type(statvalue) == "boolean" then
local boolvalue = Instance.new("BoolValue")
boolvalue.Name = statname
boolvalue.Value = statvalue
boolvalue.Parent = NotShowing
elseif type(statvalue) == "string" then
local stringvalue = Instance.new("StringValue")
stringvalue.Name = statname
stringvalue.Value = statvalue
stringvalue.Parent = NotShowing
end
end
end
local function loadData(Player)
local Data
local s, e = pcall(function()
Data = DataStore:GetAsync("UserId:"..Player.UserId)
end)
if s then
print("Getting "..Player.Name.."'s data was successful.")
else
warn("Something went wrong when loading "..Player.Name.."'s data.")
end
if Data then
for statname, statvalue in pairs(Data) do
Player.leaderstats[statname].Value = statvalue
end
print(Player.Name.."'s data has been loaded.")
else
print(Player.Name.." has no data! Generating new data.")
end
end
local function saveData(Player)
if RunService:IsStudio() then return end
local Data = {}
for _, stat in pairs(Player.leaderstats:GetChildren()) do
Data[stat.Name] = stat.Value
end
local s, e = pcall(function()
DataStore:SetAsync("UserId"..Player.UserId, Data)
end)
if s then
print(Player.Name.."'s data has been successfully saved.")
else
warn("Something went wrong while saving "..Player.Name.."'s data.")
end
end
--
--
--
Players.PlayerAdded:Connect(function(Player)
playersavetable[Player] = tick()
loadStarterData(Player)
loadData(Player)
end)
Players.PlayerRemoving:Connect(function(Player)
saveData(Player)
end)
while true do
wait(1)
for _, Player in pairs(Players:GetPlayers()) do
if tick() - playersavetable[Player] >= AUTO_SAVE_INTERVAL then
saveData(Player)
playersavetable[Player] = tick()
end
end
end

1 comment
One thing is the max leader stats you can have is 4.