Datastore Inventory

CODE:

 local ds = game:GetService("DataStoreService")
local dataStore = ds:GetDataStore("DataThing")

game.Players.PlayerRemoving:connect(function(plr)
    local plrFolder = game.ServerStorage:FindFirstChild(plr.UserId.."Data")
    if plrFolder then
        local data = {}
        data.Equipped = plrFolder.Equipped.Value
        dataStore:SetAsync(plr.UserId,data)
    end
end)

game.Players.PlayerAdded:connect(function(plr)
    local data = dataStore:GetAsync(plr.UserId)
    if data then
        local equipped = data.Equipped
    else
        print("Player has no data")
    end
end)

my friend helped me script this its for an inventory GUI and saves what you have equipped, could someone explain it to me? i don’t know how to finish the script. Here’s the GUI

image

2 Likes

Alright, so the first two lines retrieve the DataStore – Line 1 gets DataStoreService, and Line 2 gets the specific DataStore you want. If the latter doesn’t exist at the time of it being called, it gets created.

PlayerAdded

PlayerAdded is a signal that fires when a player joins the game.
Firstly it retrieves the DataStore value currently saved under a key equivalent to plr.UserId.

It then checks to see if the GetAsync actually returned anything. If it didn’t, that usually means the player hasn’t had any data saved yet (and that’s what gets printed).

If the player did have data, it creates a variable called equipped and sets it to whatever value was stored under data.Equipped.

PlayerRemoving

The PlayerRemoving signal happens when a player leaves the game.
In this instance, it creates a variable called plrFolder that points to a folder in ServerStorage that holds their data.

On the next line, it checks to see if this folder was found. The code inside the if statement would cause an error and break the code if it hadn’t found the plrFolder.
This code creates a variable called data, and sets it to a table. On the next line it sets a value in the table with the key Equipped to the value plrFolder.Equipped.Value points to (in this instance, the object named ā€˜Equipped’ should be a StringValue, and would set the Equipped value in the table to a string).

Finally it just sets the value that is stored the key equivalent to plr.UserId in the DataStore to whatever you provide next, in this case, data.


Useful?

2 Likes

Thank you but I don’t understand how I’m supposed to set a gui to a folder

It might be more wise to utilize JSON Encoding/Decoding as well as UpdateAsync.

Additionally, I’m not sure I understand what you mean in your question ā€œhow I’m supposed to set a gui to a folderā€ - do you think you can clarify this a bit more?

3 Likes

As you can see from above, the Equipped thing is the frame in the GUI

You should explain your question a bit more - what do you want to do exactly?

I want to fix the script and make it actually save, basically, How do i save data to this?

Well the script does work.

Tell us what exactly do you want to do (use your four Ws - what, when, where, why).

There is no plrFolder and no equipped value

We can’t help you if you don’t answer my question lol

What are your even trying to do? How do you want it to work? Why do you want it?

I want the save the items a player has inside his gui. Everytime playe buys item, a gui is cloned into his inventory gui and then i want to save the guis inside (his tools)

Well you’d loop through all of the things he has in the GUI and send them to the server.

Here is a primitive example:

LocalScript

-- Inside the object that holds InventoryFrame
local Frame = script.Parent.InventoryFrame.List
local Event = game.ReplicatedStorage.DataEvent -- a RemoteEvent named 'DataEvent' in ReplicatedStorage
local Items = {}

for _,Item in pairs (Frame:GetChildren()) do
    local ItemName = Item.ItemNameLabel -- a TextLabel in the the item frame
	table.insert(Items,ItemName.Text)
end

Event:FireServer(Items)

Script

local DataStore = game:GetService("DataStoreService"):GetDataStore("DataThing")
local Event = game.ReplicatedStorage.DataEvent

Event.OnServerEvent:Connect(function(Player,Data)
    DataStore:SetAsync(Player.UserId,Data)
end)
1 Like

thank you

That is easily exploitable and not the best way to approach inventory. Just handle the inventory on the server, and then replicate it to the client. Not the other way around.

3 Likes

How do I do that

Could you give an example of a JSON inventory and how you would put items into it and take them out? I’m interested as someone else suggested it, but I can’t find much info on this topic.

A JSON inventory??? Roblox or what

Of course Roblox, I’d like to know how to use tables and JSON encoding/decoding to make an inventory that’s not just a bunch of gui’s and values.

@AlreadyPro I wrote it like that because you can’t just throw people into concepts like this. You need to help them understand roughly how it works first, then you need to explain the client/server relationship then the methods you can use.

@vsnry Pro tip: Make your own post in the future :wink:

Roblox has two methods in HttpService, JSONEncode() and JSONDecode().

JSONEncode simply converts the given table to a string - it’ll look similar to this:

"{1:'value','key':'value'}"

JSONDecode takes the string as the argument and converts it into a table. Basically, JSON is just normal table manipulation with the encoding/decoding right before/after the DataStore API is used.

An example:

local function Save(Key,Data) -- Assuming Data is a table
    Data = HttpService:JSONEncode(Data)
    DataStore:SetAsync(Key,Data)
end

local function Load(Key)
    local Data = DataStore:GetAsync(Key)
    Data = HttpService:JSONDecode(Data)
    return Data
end
2 Likes