Hello, developers!
I’m having an issue with getting data from DataStore2. When I tried to print the values from :Get() when the player leaves, the values print. But then when the player joins it doesn’t print anything. What am I doing wrong?
local DataStore2 = require(1936396537)
game.Players.PlayerAdded:Connect(function(player)
local sPlayer = game:GetService("ServerStorage").PlayersFolder:WaitForChild(player.Name)
local sBackpack = sPlayer:WaitForChild("Backpack")
local sItemsEquipped = sPlayer:WaitForChild("Equipped")
local sOwned = sPlayer:WaitForChild("ItemsOwned")
local sTrailsOwned = sPlayer:WaitForChild("TrailsOwned")
local sTrailEquipped = sPlayer:WaitForChild("TrailEquipped")
local function createDefault()
local userData = {
Backpack = {};
ItemsOwned = {};
TrailsOwned = {};
Equipped = sPlayer.Equipped.Value;
TrailEquipped = sPlayer.TrailEquipped.Value;
}
return userData
end
local DataStore = DataStore2("Items", player)
local Data = DataStore:Get(createDefault())
if Data then
if Data.Equipped then
sItemsEquipped.Value = Data.Equipped
end
if Data.TrailEquipped then
sTrailEquipped.Value = Data.TrailEquipped
end
if Data.Backpack then
for i,v in pairs(Data.Backpack) do
local t = Instance.new("BoolValue")
t.Name = v
t.Parent = sBackpack
end
end
if Data.ItemsOwned then
for i,v in pairs(Data.ItemsOwned) do
local t = Instance.new("BoolValue")
t.Name = v
t.Parent = sOwned
end
end
if Data.TrailsOwned then
for i,v in pairs(Data.TrailsOwned) do
local t = Instance.new("BoolValue")
t.Name = v
t.Parent = sTrailsOwned
end
end
end
player.CharacterAppearanceLoaded:Connect(function(char)
wait(1)
for i,v in pairs(sBackpack:GetChildren()) do
local tool = game:GetService("ServerStorage").Tools:FindFirstChild(v.Name)
if tool then
local toolClone = tool:Clone()
toolClone.Parent = player.Backpack
end
end
if sTrailEquipped.Value ~= "" then
local trail0 = game:GetService("ServerStorage").Trails:FindFirstChild(sTrailEquipped.Value)
if trail0 then
local trail = trail0:Clone()
trail.Parent = char.Head
local attachment0 = Instance.new("Attachment",char.Head)
attachment0.Name = "TrailAttachment0"
local attachment1 = Instance.new("Attachment",char.HumanoidRootPart)
attachment1.Name = "TrailAttachment1"
trail.Attachment0 = attachment0
trail.Attachment1 = attachment1
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local PlayerFolder = game:GetService("ServerStorage").PlayersFolder:WaitForChild(player.Name)
local DataStore = DataStore2("Items", player)
local Save = {
Backpack = {};
ItemsOwned = {};
TrailsOwned = {};
Equipped = PlayerFolder.Equipped.Value;
TrailEquipped = PlayerFolder.TrailEquipped.Value;
}
local success, err = pcall(function()
for i,v in pairs(PlayerFolder.Backpack:GetChildren()) do
table.insert(Save.Backpack, v.Name)
end
for i,v in pairs(PlayerFolder.ItemsOwned:GetChildren()) do
table.insert(Save.ItemsOwned, v.Name)
end
for i,v in pairs(PlayerFolder.TrailsOwned:GetChildren()) do
table.insert(Save.TrailsOwned, v.Name)
end
DataStore:Set(Save)
end)
if not success then
warn(err)
end
end)