Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How do I datastore items/strings? [closed]

Asked by 5 years ago

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!

0
This is a great question! I am writing an answer right now. User#24403 69 — 5y
0
Could you please accept my answer if my solution worked? User#24403 69 — 5y

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?

2 answers

Log in to vote
6
Answered by 5 years ago
Edited 5 years ago

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.


Hopefully this answered your question, and if it did, then don't forget to hit that "Accept Answer" button. If you have any other questions, then feel free to leave them down in the comments.
0
This was excellent, thank you so much! ii_FireDev 10 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

Hey ii_FireDev,

First off, storing tables is pretty simple to do in datastores. If you want more information on how to do that, just go to this video. It's an old video but, the knowledge it impends on you should still be useful.

Now, saving strings is just the same as saving integer values. All you have to do is put string values inside of the table.

Well, I hope i helped and have a wonderful day/night.

Thanks,

Best regards,

KingLoneCat

5
why would you say thanks in your own answer User#24403 69 — 5y
0
Thanks for putting time into reading the answer. That's why. KingLoneCat 2642 — 5y