I am fairly new to data storing and I only know how to datastore a leaderstat value. I have no idea how to datastore strings or tables, what I am trying to make is a shop that saves items essentially. I know that there are multiple ways to go about doing this, I just don't have enough knowledge of data storing to come about doing it. Any help is greatly appreciated!
What I did is put a Folder in ServerStorage, and name it something like playerTools
. This will be where the tools of the player are stored in. And another folder named shopItems
or something along those lines. These are the tools the players can buy.
When they join, I just make another folder inside that tools folder, and the name of that folder is just the player's name.
local toolsFolder = Instance.new("Folder") toolsFolder.Name = client.Name toolsFolder.Parent = playerTools
And if your shop is a GUI shop, you'll most likely have remote events and functions. So when they buy the tool, just make a clone of that tool, and put it in their folder.
local buyRemote = ... -- the remote function that lets a player buy the tool local ServerStorage = game:GetService("ServerStorage") local shopItems = ServerStorage.shopItems buyRemove.OnServerInvoke = function(client, request) local tool = shopItems:FindFirstChild(tostring(request)) -- just to make sure legitimate information is being sent if tool and not ServerStorage.playerTools[client.Name]:FindFirstChild(tool.Name) then -- You will probably want to do more stuff, such as check if they have enough money. I'm leaving that all to you. tool:Clone().Parent = ServerStorage.playerTools[client.Name] return true -- return back to the client everything went fine. else return false -- already owns the tool, sent bad information, not enough money, etc end end
Here I am just doing sanity checks to make sure nothing breaks if bad information is being sent. I check if they don't have the tool already so no duplicate purchases can be made. I then clone the tool into that folder.
Now to load the data. Iterate over the children of the folder, and since you cannot store userdata in Data Stores (Instances, Vector3 values, etc) I won't save the tools themselves. But the names of the tools. Since you can save tables and strings to data stores this will work perfect.
local toolsFolder = Instance.new("Folder") toolsFolder.Name = client.Name toolsFolder.Parent = playerTools local tools = toolSave:GetAsync("tools-" .. client.UserId) if type(tools) == "table" then -- if there is a table for _, v in ipairs(tools) do local tool = ServerStorage.shopItems[v]:Clone() tool.Parent = client.Backpack end end
Now when they leave, do something similar. Iterate over the children of their folder, insert the names of the tools into the tools
table.
local tools = {} -- we will save this to the datastore for _, tool in ipairs(playerTools[client.Name]:GetChildren()) do table.insert(tools, tool.Name) end toolSave:UpdateAsync("tools-" .. client.UserId, function() return tools -- update data with tools end) playerTools[client.Name]:Destroy()
Then save that table and finally, destroy their folder since it is no longer needed.
Hey ii_FireDev,
Thanks,
Best regards,
KingLoneCat
Locked by User#24403
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?