Hi there!
I’m currently working on a basic saving system. It should (when working correctly), save the player’s current spawn point (vector3), their current area (string), and values for a few bonfires (string and bool stored inside a dictionary). When testing, it appears as though the script saves the values correctly. However, upon joining the game, I am only able to get the bonfire values, not the current area or spawn
The items the game gets from the datastore
The items the game saves to the datastore
As you can see from the screenshots above, the game doesn’t get the Player_Saved_Area or the Player_Saved_Spawn. Any help you can provide would be greatly appreciated.
Script
-- Get the data service
local Data_Service = game:GetService("DataStoreService")
-- Get the name of the datastore we save our values to
local MyData = Data_Service:GetDataStore("Ro_Souls_Script_Rewrite_Data")
-- Get the folder with all the bonfires
local Bonfire_Folder = game.Workspace.Bonfires
local function Create_Save_Items(player)
-- Create the save folder
local Player_Saved_Items = Instance.new("Folder", player)
Player_Saved_Items.Name = "Save_Folder"
-- Create the saved area string
local Current_Area = Instance.new("StringValue", Player_Saved_Items)
Current_Area.Name = "Current_Area"
Current_Area.Value = ""
-- Create the respawn point
local Respawn_Point = Instance.new("Vector3Value", Player_Saved_Items)
Respawn_Point.Name = "Respawn_Point"
Respawn_Point.Value = Vector3.zero
-- Get saved data
local data = MyData:GetAsync(player.UserId)
-- Set data to saved data
if data then
-- Get the current area
if data['Player_Saved_Area'] then
Current_Area.Value = data['Player_Saved_Area']
else
Current_Area.Value = ""
end
-- Get the current respawn point
if data['Player_Saved_Spawn'] then
Respawn_Point.Value = Vector3.new(table.unpack(data['Player_Saved_Spawn']))
else
Respawn_Point.Value = Vector3.zero
end
else
Current_Area.Value = ""
Respawn_Point.Value = Vector3.zero
end
end
local function Create_Save_Bonfires(player)
-- Find the save folder
local Player_Saved_Items = player:WaitForChild("Save_Folder")
-- Create the bonfire folder
local Player_Saved_Bonfire = Instance.new("Folder", Player_Saved_Items)
Player_Saved_Bonfire.Name = "Bonfire_Save_Folder"
-- Create the bonfires
for _, bonfire in Bonfire_Folder:GetChildren() do
if bonfire:IsA("Model") then
-- Create the bonfire folder
local Saved_Bonfire = Instance.new("Folder", Player_Saved_Bonfire)
Saved_Bonfire.Name = bonfire.Bonfire_Name.Value
-- Create the discovered bool
local Discovered = Instance.new("BoolValue", Saved_Bonfire)
Discovered.Name = 'Discovered'
end
end
-- Get saved data
local data = MyData:GetAsync(player.UserId)
-- Set data to saved data
if data then
-- Set the discovered bool to true
for _, bonfire in Player_Saved_Bonfire:GetChildren() do
for index, value in ipairs(data) do
if value[1] == bonfire.Name then
bonfire.Discovered.Value = value[2]
end
end
end
else
-- Set the discovered bool to false
for _, bonfire in Player_Saved_Bonfire:GetChildren() do
bonfire.Discovered.Value = false
end
end
end
local Join_Connection
Join_Connection = game.Players.PlayerAdded:Connect(function(player)
local data = MyData:GetAsync(player.UserId)
if data then
print(data)
end
-- Create the core values
Create_Save_Items(player)
-- Create the bonfires
Create_Save_Bonfires(player)
-- Terminate connection
Join_Connection:Disconnect()
end)
local Leave_Connection
Leave_Connection = game.Players.PlayerRemoving:Connect(function(player)
-- Get the saved area string
local Current_Area = player.Save_Folder.Current_Area.Value
-- Get the respawn point vector 3
local Respawn_Point = player.Save_Folder.Respawn_Point.Value
-- Get the bonfire folder
local Player_Saved_Bonfire = player.Save_Folder.Bonfire_Save_Folder
-- Create a list of data to save
local Data_To_Save = {
Player_Saved_Area = Current_Area,
Player_Saved_Spawn = {Respawn_Point.X, Respawn_Point.Y, Respawn_Point.Z}
}
-- Add the bonfires
for _, bonfire in Player_Saved_Bonfire:GetChildren() do
local New_Bonfire_Data = {bonfire.Name, bonfire.Discovered.Value}
table.insert(Data_To_Save, New_Bonfire_Data)
end
-- Sync the saved data
local success, err = pcall(function()
MyData:SetAsync(player.UserId, Data_To_Save)
end)
if not success then
print("data failed to save!")
end
-- Terminate connection
Leave_Connection:Disconnect()
end)