I've been trying to fix this script for quite a while now and I cannot find a fix for it. I have no idea what is wrong with the code. Every time I load my data it does not save.
local MyDatastore = game:GetService("DataStoreService"):GetDataStore("MyDatastore")
local IntValues = {
["Cash"] = 0;
}
local BoolValues = {
["TestBool"] = false;
}
local StringValue = {
["TestString"] = "";
}
game.Players.PlayerAdded:Connect(function(Player)
local DataFolder = Instance.new("Folder",Player)
DataFolder.Name = "leaderstats"
local Inventory = Instance.new("Folder",Player)
Inventory.Name = "Inventory"
for i,v in pairs(IntValues) do
local Value = Instance.new("IntValue",DataFolder)
Value.Name = i
end
for i,v in pairs(BoolValues) do
local Value = Instance.new("BoolValue",DataFolder)
Value.Name = i
end
for i,v in pairs(StringValue) do
local Value = Instance.new("StringValue",DataFolder)
Value.Name = i
end
local Data
local Success,Fail = pcall(function()
Data = MyDatastore:GetAsync(Player.UserId)
end)
if Success then
print("Successfully loaded "..Player.Name.."'s data!")
for i,v in pairs(Data) do
for index,value in pairs(DataFolder:GetChildren()) do
if v[1] == value.Name then
if type(v[2]) == "number" then
v[2] = value.Value
print(v[1],v[2])
end
if type(v[2]) == "string" then
v[2] = value.Value
print(v[1],v[2])
end
if type(v[2]) == "boolean" then
v[2] = value.Value
print(v[1],v[2])
end
end
end
end
elseif Fail then
for i,v in pairs(DataFolder) do
if v:IsA("IntValue") then
v.Value = 0
end
if v:IsA("StringValue") then
v.Value = ""
end
if v:IsA("BoolValue") then
v.Value = false
end
end
warn("Did not successfully load "..Player.Name.."'s data!")
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local SavingTable = {}
for i,v in pairs(Player.leaderstats:GetChildren()) do
local NewTable = table.create(1)
table.insert(NewTable,1,v.Name)
table.insert(NewTable,2,v.Value)
table.insert(SavingTable,NewTable)
end
for i,v in pairs(SavingTable) do
for index,value in pairs(v) do
print(value)
end
end
local Success,Fail = pcall(function()
MyDatastore:SetAsync(Player.UserId,SavingTable)
end)
if Success then
print("Saved "..Player.Name.."'s data!")
elseif Fail then
warn("Did not Save "..Player.Name.."'s data!")
end
end)

1 comment
my answer worked?