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
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.
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?
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)
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.
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.
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.
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