Yes, I put it at the beginning. Should it be at the end?
game:BindToClose(function()
task.wait(2)
for _, player in pairs(game.Players:GetPlayers()) do
local itemTable = {}
for _, item in pairs(player.UserItems:GetChildren()) do
table.insert(itemTable,item.Name)
print(item.Name.. " saved to UserId "..player.UserId)
end
local success, errorMsg = pcall(function()
DataStore:SetAsync("UserItems-"..player.UserId,itemTable)
end)
if success then
print("Saved!")
else
print(errorMsg)
end
end
end)
I’m gonna put the task.wait(2) in both scripts to be sure.
Would it be because there’s so many data stores in my game? I just realized I have quite a bit lol
There are four current data stores that are saving when the player leaves. Settings, Easter Eggs, Purchased Items and Equipped Items
Oh you know what? It’s probably because I’m making a new BoolValue, and because it’s a server script, it doesn’t even know it’s there because it wasn’t there when the server started.
Edit: nevermind, the easter egg script does this and it works
after a post with 26 comments I found the issue, you need to wait for the players character.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("EquippedUserItems")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
local data
local success, errorMsg = pcall(function()
data = DataStore:GetAsync("EquippedUserItems-"..player.UserId)
end)
if success and data then
for _, itemName in pairs(data) do
local sword = game:GetService("ReplicatedStorage"):WaitForChild("ShopItems").Items[itemName]:Clone()
sword.Parent = player.Backpack
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local itemTable = {}
for _, item in pairs(player.Backpack:GetChildren()) do
table.insert(itemTable,item.Name)
print(item.Name.. " saved to UserId "..player.UserId)
end
local success, errorMsg = pcall(function()
DataStore:SetAsync("EquippedUserItems-"..player.UserId,itemTable)
end)
if success then
print("Saved!")
else
print(errorMsg)
end
end)
game:BindToClose(function()
task.wait(2)
end)
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("EquippedUserItems")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
local data
local success, errorMsg = pcall(function()
data = DataStore:GetAsync("EquippedUserItems-"..player.UserId)
end)
if success and data then
for _, itemName in pairs(data) do
game:GetService("ReplicatedStorage"):WaitForChild("ShopItems").Items[itemName]:Clone().Parent = player.Backpack
end
end
player.CharacterRemoving:Connect(function(char)
char.Humanoid:UnequipTools()
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local plrbackpack = player:FindFirstChild("Backpack")
local itemTable = {}
for _, item in pairs(plrbackpack:GetChildren()) do
table.insert(itemTable,item.Name)
print(item.Name.. " saved to UserId "..player.UserId)
end
local success, errorMsg = pcall(function()
DataStore:SetAsync("EquippedUserItems-"..player.UserId,itemTable)
end)
if success then
print("Saved!")
else
print(errorMsg)
end
end)
game:BindToClose(function()
task.wait(2)
end)
That did not fix it, and I think I know why. When the player is in the shop, it makes a new BoolValue in a LocalScript.
local new = Instance.new("BoolValue")
new.Name = PublicItemID
new.Parent = player.UserItems
My guess is there’s some client/server thing going on here. I could be dead wrong though xD
Edit: The item is also given to the player through the same script.
Edit 2: I’m gonna try to make an event that gives this BoolValue to the player through a server script in ServerScriptService
It still doesn’t explain the currently equipped items though
Alright, so the player items are now working! It’s still not giving the player their items back when they rejoin, but I can go into the shop and equip it. I’ll take a look around to see if I can fix this myself. Thanks so much guys! If I have any more problems with this, I’ll reply here again lol.
Or maybe I should just create a new thread, it doesn’t really link to my main issue.