EDIT: The script now magicly works
Hello, I have a datastore script that is not saving data in “Inventory” and “Stats”
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("RCdata2")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Data = {
Strength = 10;
Rebirths = 0;
Cash = 10;
Speed = 16;
Damage = 5;
Health = 100;
Weight = 1;
Gems = 0;
BlueColor = false;
PinkColor = false;
GreenColor = false;
RedColor = false;
RedSword = false;
BlueSword = false;
OmegaSword = false;
DarkSword = false;
EquipSword = "default";
EquipColor = "default"
}
local playersavetable = {};
local function loadStarterData(Player)
local Stats = Instance.new("Folder")
Stats.Name = "Stats"
Stats.Parent = Player
local Inventory = Instance.new("Folder")
Inventory.Name = "Inventory"
Inventory.Parent = Player
for statname, statvalue in pairs(Data) do
if type(statvalue) == 'number' then
local intvalue = Instance.new("IntValue")
intvalue.Name = statname
intvalue.Value = statvalue
intvalue.Parent = Stats
else if type(statvalue) == 'boolean' then
local intvalue = Instance.new("BoolValue")
intvalue.Name = statname
intvalue.Value = statvalue
intvalue.Parent = Inventory
else if type(statvalue) == 'string' then
local intvalue = Instance.new("StringValue")
intvalue.Name = statname
intvalue.Value = statvalue
intvalue.Parent = Inventory
end
end
end
end
end
local function loadData(player, Stats, Inventory)
local Data = {}
local Stats = Stats or player.Stats
local Inventory = player.Inventory
local s, e = pcall(function()
Data = DataStore:GetAsync('UserId'..player.UserId)
end)
if s then
print (player.Name.."Data loaded")
else
print(player.Name.."Data failed to load")
end
if Data then
for statname, statvalue in pairs(Data) do
if type(statvalue) == "number" then
player.Stats[statname].Value = statvalue
else if type(statvalue) == "boolean" then
player.Inventory[statname].Value = statvalue
else if type(statvalue) == "string" then
player.Inventory[statname].Value = statvalue
end
end
end
end
print(player.Name.."Data has been loaded")
else
print(player.Name.."No data found! generating..")
end
end
local function saveData(player)
-- if RunService:IsStudio() then return end
local Data = {}
for _, stat in ipairs(player.Stats:GetChildren()) do
if not stat:IsA("Folder") then
Data[stat.Name] = stat.Value
end
end
for _, stat in ipairs(player.Inventory:GetChildren()) do
if not stat:IsA("Folder") then
Data[stat.Name] = stat.Value
end
end
local s, e = pcall(function()
DataStore:SetAsync('UserId'..player.UserId, Data)
end)
if s then
print(player.Name.."Data has been saved")
else
warn (player.Name.."Data failed to save"..e)
end
end
ReplicatedStorage.SaveEvent.OnServerEvent:Connect(function(player)
saveData(player)
end)
Players.PlayerAdded:Connect(function(player)
playersavetable[player] = tick()
loadStarterData(player)
loadData(player)
local Stats = player:WaitForChild("Stats")
local Inventory = player:WaitForChild("Inventory")
local RemoveEvent
RemoveEvent = Players.PlayerRemoving:Connect(function(vplayer)
if vplayer == player then
saveData(vplayer, Stats, Inventory)
RemoveEvent:Disconnect()
end
end)
end)
I have tried editing the script for a while now but no luck so far, I have a feeling I made a silly mistake that causes the data in “Inventory” and “Stats” to not save.
An example of the issue is “BlueSword” being sat to true via RemoteEvent and then I leave and rejoin and its back to false (like default)
This issue all started when I tried adding “Inventory” to the datastore as well as stringvalues, before that it worked just fine!