I feel or may be doing this code completely wrong, but. So Instead of me pasting addItem() 103+ times, is there a quicker way to do that in a few lines of code and save that much data. Heres so far on my code which forces me to paste addItem() 103 times. I also have no idea how to quickly save it all when the player leaves.
local inv = getInventory(char)
local invData = game:GetService("DataStoreService"):GetDataStore("inventory-main")
local invDatakey = "data-"..plr.UserId
local GetSave = invData:GetAsync(invDatakey)
if GetSave then
local dataToLoad = S_H:JSONDecode(GetSave)
addItem(inv,dataToLoad["Slot 1"].name, dataToLoad["Slot 1"].count)
local alert = script.alert:Clone()
alert.alert.Text = "Successfully Loaded your inventory from last gameplay save!"
alert.Parent = plr.PlayerGui.HUDGui.Notification
print("Data saved for "..plr.Name.."'s inventory loaded!")
else
local alert = script.alert:Clone()
alert.alert.Text = "No data saved, loading default data"
alert.Parent = plr.PlayerGui.HUDGui.Notification
local newData = {
["Slot 1"] = {
name = "GrassBlock";
count = 1
}
}
local dataToSave = S_H:JSONEncode(newData)
invData:SetAsync(invDatakey, newData)
local alert = script.alert:Clone()
alert.alert.Text = "Successfully Loaded your inventory from last gameplay save!"
alert.Parent = plr.PlayerGui.HUDGui.Notification
print("Data Saved for ".. plr.Name)
print("Data saved for "..plr.Name.."'s inventory loaded!")
end
Personally, I use JSONEncode to group together a bunch of data. I use DataStore2 to save the data. Although Encoding it in JSON is not required, I like doing so.
In-game player has aprox 103+ plus slots, the data is stored in a folder with string values json encoded, When player leaves I need to save that data, also when the player connects I need to load the data.
My brain hurts looking at this code trying to load & save the data.
local inv = getInventory(char)
local invData = game:GetService("DataStoreService"):GetDataStore("inventory-main")
local invDatakey = "data-"..plr.UserId
local GetSave = invData:GetAsync(invDatakey)
if GetSave then
local dataToLoad = S_H:JSONDecode(GetSave)
for i, item in pairs(dataToLoad) do
addItem(inv, dataToLoad["Slot "..i].name, dataToLoad["Slot "..i].count)
end
local alert = script.alert:Clone()
alert.alert.Text = "Successfully Loaded your inventory from last gameplay save!"
alert.Parent = plr.PlayerGui.HUDGui.Notification
print("Data saved for "..plr.Name.."'s inventory loaded!")
else
local alert = script.alert:Clone()
alert.alert.Text = "No data saved, loading default data"
alert.Parent = plr.PlayerGui.HUDGui.Notification
local newData = {
["Slot 1"] = {
name = "GrassBlock";
count = 1
}
}
local dataToSave = S_H:JSONEncode(newData)
invData:SetAsync(invDatakey, dataToSave)
local alert = script.alert:Clone()
alert.alert.Text = "Successfully Loaded your inventory from last gameplay save!"
alert.Parent = plr.PlayerGui.HUDGui.Notification
print("Data Saved for ".. plr.Name)
print("Data saved for "..plr.Name.."'s inventory loaded!")
end