So i want to put a value in the folder which is a item ingame and want to save it and load it when they join back and i need help i never done it and here’s a script its a bit messy because its been a while snice I tested!
local DataStoreService = game:GetService("DataStoreService")
local mydatastore = DataStoreService:GetDataStore("DataStore")
local RunService = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(Player)
local Inv = Instance.new("Folder")
Inv.Name = "Inventory"
Inv.Parent = Player
local data
local success, EM = pcall(function()
data = mydatastore:GetAsync(Player.UserId.."Inventory")
end)
if success then
local value = Instance.new("BoolValue")
value.Parent = Inv
value.Name = data
print("Data Lodaded")
else
print("There was an error getting ur data")
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local success, EM = pcall(function()
for i,v in pairs(Player.Inventory:GetChildren())do
print(v)
mydatastore:SetAsync(Player.UserId.."Inventory",v.Name)
end
end)
if success then
print("Player Data successfully saved!")
else
print("There was An Error on Saving")
warn(EM)
end
end)
1 Like
It would be harder to explain than to fix the code itself, so here you go.
None of this was tested, so it might error.
local folderItems --a folder to store your items to be later cloned from
local DataStoreService = game:GetService("DataStoreService")
local inventorystore = DataStoreService:GetDataStore("Inventory")
local RunService = game:GetService("RunService")
local listErrored = {}
--added :GetService
game:GetService("Players").PlayerAdded:Connect(function(Player)
local Inv = Instance.new("Folder")
Inv.Name = "Inventory"
Inv.Parent = Player
local data
local success, errmsg
--retry 5 times
for i=1,5 do
success, errmsg = pcall(function()
data = inventorystore:GetAsync(Player.UserId)
end)
if success then
break
end
end
if success then
--added loading
for _,name in ipairs(data) do
local obj = folderItems:FindFirstChild(name):Clone()
obj.Parent = Inv
end
else
--Remade all of this block, you should also add a visible message that notifies that an error
--occured and that the data won't be saved.
listErrored[Player] = true
error("Failed to load player "..Player.Name.."'s inventory with error code:\n"..errmsg)
end
end)
--added :GetService again
game:GetService("Players").PlayerRemoving:Connect(function(Player)
--avoid saving if player errored
if listErrored[Player] then
listErrored[Player] = nil
return
end
local list = {}
for i,v in ipairs(Player.Inventory:GetChildren())do
list[i] = v.Name
end
local success, EM
for i=1,5 do
success, errmsg = pcall(function()
inventorystore:UpdateAsync(Player.UserId,function(_) return list end)
end)
if success then
break
end
end
if success then
print("Successfully saved player "..Player.Name.."'s inventory.")
else
error("Failed to save player "..Player.Name.."'s inventory with error:\n"..errmsg)
end
end)
@nooneisback ahh thx btw this part is experiencing some issues right here and i don’t understand the part here its new to me a little
local folderItems --a folder to store your items to be later cloned from
local DataStoreService = game:GetService("DataStoreService")
local inventorystore = DataStoreService:GetDataStore("Inventory")
local RunService = game:GetService("RunService")
local listErrored = {}
--added :GetService
game:GetService("Players").PlayerAdded:Connect(function(Player)
local Inv = Instance.new("Folder")
Inv.Name = "Inventory"
Inv.Parent = Player
local data
local success, errmsg
--retry 5 times
for i=1,5 do
success, errmsg = pcall(function()
data = inventorystore:GetAsync(Player.UserId)
end)
if success then
break
end
end
if success then
--added loading
for _,name in ipairs(data) do
local obj = folderItems:FindFirstChild(name):Clone() -- Error location I 'confused it says "ServerScriptService.SaveSytem:30: attempt to index nil with 'FindFirstChild'
obj.Parent = Inv
end
else
--Remade all of this block, you should also add a visible message that notifies that an error
--occured and that the data won't be saved.
listErrored[Player] = true
error("Failed to load player "..Player.Name.."'s inventory with error code:\n"..errmsg)
end
end)
Because you need to create some kind of a folder to store every item type and then assign it to the folderItems variable.
Basically an item storage.
ahh so i have to assign the value to a folder write alright
1 Like