Hi! I need help about the datastore i am using to load things as expected to be…
I mean I’ll add stuff as StringValue from Server when Quest is done etc. but From what i have found…
It saves as i want it to be but Doesn’t load…
Example:
I add item or something else to Inventory
it saves
but doesn’t load
only loads already existing stats.
Here is the script:
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverScriptService = game:GetService("ServerScriptService")
local serverSettings = replicatedStorage.Stats:WaitForChild("AboutServer")
local module = {}
function module.Save(player)
if players:FindFirstChild(player.Name) and player:FindFirstChild("IsLoaded") then
local status, message = pcall(function()
local Achievements = {}
for _,achievements in pairs(player.Achievements:GetChildren()) do
if achievements:IsA("ValueBase") then
table.insert(Achievements, {achievements.Name, achievements.Value})
end
end
local Equipped = {}
for _,equipped in pairs(player.Equipped:GetChildren()) do
if equipped:IsA("ValueBase") then
table.insert(Equipped, {equipped.Name, equipped.Value})
end
end
local Inventory = {}
for _,inventorie in pairs(player.Inventory:GetChildren()) do
if inventorie:IsA("ValueBase") then
table.insert(Inventory, {inventorie.Name, inventorie.Value})
end
end
local Stats = {}
for _,stats in pairs(player.Stats:GetChildren()) do
if stats:IsA("ValueBase") then
table.insert(Stats, {stats.Name, stats.Value})
end
end
local BasicStuff = {}
for _,basicstuff in pairs(player.BasicStuff:GetChildren()) do
if basicstuff:IsA("ValueBase") then
table.insert(BasicStuff, {basicstuff.Name, basicstuff.Value})
end
end
local Titles = {}
for _,titles in pairs(player.Titles:GetChildren()) do
if titles:IsA("ValueBase") then
table.insert(Titles, {titles.Name, titles.Value})
end
end
local Quests = {}
for _,quests in pairs(player.Quests:GetChildren()) do
if quests:IsA("ValueBase") then
table.insert(Quests, {quests.Name, quests.Value})
end
end
local CompletedQuests = {}
for _,completedquests in pairs(player.CompletedQuests:GetChildren()) do
if completedquests:IsA("ValueBase") then
table.insert(CompletedQuests, {completedquests.Name, completedquests.Value})
end
end
local saveFile = {Achievements, Equipped, Inventory, Stats, BasicStuff, Titles, Quests, CompletedQuests}
game:GetService("DataStoreService"):GetDataStore("ZenithOfZeroth"):SetAsync(player.UserId, saveFile)
end)
if status ~= true then
print(player.Name .. " couldn't save, retrying... \n" .. message)
wait(10)
module.Save(player)
else
print(player.Name .. " saved successfully!")
end
end
end
function module.Load(player)
if players:FindFirstChild(player.Name) then
local status = pcall(function()
local saveFile = game:GetService("DataStoreService"):GetDataStore("ZenithOfZeroth"):GetAsync(player.UserId)
if saveFile then
if saveFile[1] then
for _,achievements in pairs(saveFile[1]) do -- Achievements
if achievements[1] and achievements[2] and player.Achievements:FindFirstChild(achievements[1]) then
player.Achievements[achievements[1]].Value = achievements[2]
end
end
end
if saveFile[2] then
for _,equipped in pairs(saveFile[2]) do -- Equipments
if equipped[1] and equipped[2] and player.Equipped:FindFirstChild(equipped[1]) then
player.Equipped[equipped[1]].Value = equipped[2]
end
end
end
if saveFile[3] then
for _,inventory in pairs(saveFile[3]) do -- Inventory
if inventory[1] and inventory[2] and player.Inventory:FindFirstChild(inventory[1]) then
player.Inventory[inventory[1]].Value = inventory[2]
end
end
end
if saveFile[4] then
for _,stats in pairs(saveFile[4]) do -- Stats
if stats[1] and stats[2] and player.Stats:FindFirstChild(stats[1]) then
player.Stats[stats[1]].Value = stats[2]
end
end
end
if saveFile[5] then
for _,basicstuff in pairs(saveFile[5]) do -- BasicStuff
if basicstuff[1] and basicstuff[2] and player.BasicStuff:FindFirstChild(basicstuff[1]) then
player.BasicStuff[basicstuff[1]].Value = basicstuff[2]
end
end
end
if saveFile[6] then
for _,titles in pairs(saveFile[6]) do -- Titles
if titles[1] and titles[2] and player.Titles:FindFirstChild(titles[1]) then
player.Titles[titles[1]].Value = titles[2]
end
end
end
if saveFile[7] then
for _,quests in pairs(saveFile[7]) do -- Quests
if quests[1] and quests[2] and player.Quests:FindFirstChild(quests[1]) then
player.Quests[quests[1]].Value = quests[2]
end
end
end
if saveFile[8] then
for _,compeletedquests in pairs(saveFile[8]) do -- CompletedQuests
if compeletedquests[1] and compeletedquests[2] and player.CompletedQuests:FindFirstChild(compeletedquests[1]) then
player.CompletedQuests[compeletedquests[1]].Value = compeletedquests[2]
end
end
end
end
end)
if status ~= true then
print(player.Name .. " couldn't load, retrying...")
wait(10)
module.Load(player)
else
print(player.Name .. " loaded successfully!")
end
end
end
function module.PlayerAdded(player)
local function UpdateCharacter(noHeal)
if player.Character and player.Character:FindFirstChild("Humanoid") then
end
end
-- Don't mind this.
local function CreateObject(className, parent, properties)
local object = Instance.new(className, parent)
for _,property in pairs(properties) do
object[property[1]] = property[2]
end
return object
end
-- NO
local Achievements = CreateObject("Folder", player, {
{"Name", "Achievements"}
})
local BasicStuff = CreateObject("Folder", player, {
{"Name", "BasicStuff"}
})
local Equipped = CreateObject("Folder", player, {
{"Name", "Equipped"}
})
local Inventory = CreateObject("Folder", player, {
{"Name", "Inventory"}
})
local Stats = CreateObject("Folder", player, {
{"Name", "Stats"}
})
local Titles = CreateObject("Folder", player, {
{"Name", "Titles"}
})
local Quests = CreateObject("Folder", player, {
{"Name", "Quests"}
})
local CompletedQuests = CreateObject("Folder", player, {
{"Name", "CompletedQuests"}
})
--Folders
CreateObject("IntValue", player, {
{"Name", "InventoryLimit"},
{"Value", 40}
})
CreateObject("IntValue", player, {
{"Name", "InventoryCurrent"},
{"Value", 0}
})
--Inventory Stuff
local level = CreateObject("IntValue", BasicStuff, {
{"Name", "Level"},
{"Value", 0}
})
local xp = CreateObject("IntValue", BasicStuff, {
{"Name", "XP"},
{"Value", 0}
})
local xpneeded = CreateObject("IntValue", BasicStuff, {
{"Name", "XPNeeded"},
{"Value", 2000}
})
local gold = CreateObject("IntValue", BasicStuff, {
{"Name", "Gold"},
{"Value", 100}
})
--BasicStuff
local Points = CreateObject("IntValue", Stats, {
{"Name", "Points"},
{"Value", 1}
})
local Body = CreateObject("IntValue", Stats, {
{"Name", "Body"},
{"Value", 1}
})
local Armor = CreateObject("IntValue", Stats, {
{"Name", "Armor"},
{"Value", 1}
})
local Weapon = CreateObject("IntValue", Stats, {
{"Name", "Weapon"},
{"Value", 1}
})
local Mind = CreateObject("IntValue", Stats, {
{"Name", "Mind"},
{"Value", 1}
})
--STATS
CreateObject("StringValue", Equipped, {
{"Name", "CurrentWeapon"},
{"Value", "None"}
})
CreateObject("StringValue", Equipped, {
{"Name", "CurrentArmor"},
{"Value", "None"}
})
CreateObject("StringValue", Equipped, {
{"Name", "CurrentHelmet"},
{"Value", "None"}
})
CreateObject("StringValue", Equipped, {
{"Name", "BackAccessory"},
{"Value", "None"}
})
CreateObject("StringValue", Equipped, {
{"Name", "Ring"},
{"Value", "None"}
})
CreateObject("StringValue", Equipped, {
{"Name", "Necklace"},
{"Value", "None"}
})
CreateObject("StringValue", Equipped, {
{"Name", "Bracelet"},
{"Value", "None"}
})
--Equipments
local function UpdateXP()
while true do
wait(0.22)
if xp.Value >= xpneeded.Value and level.Value < serverSettings.LevelLimit.Value then
xp.Value = 0
level.Value += 1
Points.Value += serverSettings.PointsPerLevel.Value
else
break
end
end
end
--LEVEL STUFF
xp.Changed:Connect(UpdateXP)
UpdateXP()
level.Changed:Connect(function()
UpdateCharacter()
player.Character.Humanoid.Health = player.Character.Humanoid.MaxHealth
end)
--ON UPDATE STUFF
module.Load(player)
local tag = Instance.new("Model", player)
tag.Name = "IsLoaded"
--OK?
local function CharacterSpawned()
UpdateCharacter()
print("You have been Respawned upon the Mercy of Zenith!")
end
if player.Character then
CharacterSpawned()
end
player.CharacterAdded:Connect(CharacterSpawned)
repeat -- Autosave every 4 minutes
wait(240)
module.Save(player)
until not players:FindFirstChild(player.Name)
end
return module