I am having trouble storing data values from a player folder to Datastore. I created an empty folder named “inventory” on the player. This folder fills up with the name of objects they obtain when playing my game. In the following image example I collected 2 objects (BucketHat and Crown):
The problem is that I can only access the data through local scripts and not normal scripts. In order to save data, it must be accessible by normal scripts as you guys already know.
I save their data when players leave a game or the server closes. I tried using a remote event to send the folder data from the client to the server, but the character and its folder get destroyed before the process is completed.
I also tried saving the folder on “Replicated Storage” and “Replicated First” so I can access the data from the server-side, but it only let me do so with local scripts for some reason.
At this point I am just not sure what to do, does anyone have any suggestions or ideas on how to save the data?
The folder was made through a server script. Part of the code shown below:
game.Players.PlayerAdded:Connect(function(player)
print("Player "..player.Name.." added")
local inventory = Instance.new("Folder")
inventory.Name = "inventory"
inventory.Parent = player
end)
The objects themselves are attained through a GUI, and hence are given to the player through a local script. An example of how the crown was obtained:
local player = game.Players.LocalPlayer
script.Parent.Activated:Connect(function()
local bought = player.inventory:FindFirstChild("Crown")
if not bought then
local temp = Instance.new("BoolValue")
temp.Name = "Crown"
temp.Parent = player.inventory
end
end)
I am attempting to save the information to a Datastore through a server script:
if player:FindFirstChild("inventory") then
local accessorySave = {}
for _,acc in pairs(player.inventory:GetChildren()) do
print(" Obj: "..acc.Name)
table.insert(accessorySave,acc.Name)
end
local success, errorMessage = pcall(function()
inventoryDatastore:SetAsync("UserInventory-"..player.UserId, accessorySave) --Save
end)
if success then
print(player.Name.."'s Accessory Inventory Saved succesfully!")
else
print("Error: "..errorMessage)
end
end
script.Parent.Activated:Connect(function()
local player = script.Parent.Parent --[[because the tool can only be activated when held by a player which means it is a descendant of his character]]--
local bought = player.inventory:FindFirstChild("Crown")
if not bought then
local temp = Instance.new("BoolValue")
temp.Name = "Crown"
temp.Parent = player.inventory
end
end)
I am trying that but it won’t activate the GUI (it won’t even run a print statement when Activated). Also, it messes with a RemoteEvent I have set up for it since changing it to be server-side renders it useless (I can fix it though).I am currently trying different things.