–Loading the data
local playerStats = DataStore:GetAsync(Player.UserId)
if playerStats ~= nil then
Currency.Value = playerStats[1]
Kills.Value = playerStats[2]
Level.Value = playerStats[3]
Exp.Value = playerStats[4]
ExpNeeded.Value = playerStats[5]
--Load the gunFolder somehow
print(Player.Name.. "'s data has been loaded!")
else
ExpNeeded.Value = 1000
print(Player.Name.. " has no data!")
end
–Saving the data
local gunFolder = Player:WaitForChild("gunFolder")
local save = {}
table.insert(save, Player.leaderstats.Credits.Value)
table.insert(save, Player.leaderstats.Kills.Value)
table.insert(save, Player.leaderstats.Level.Value)
table.insert(save, Player.Exp.Value)
table.insert(save, Player.ExpNeeded.Value)
for _, weapon in ipairs(gunFolder:GetChildren()) do
table.insert(save, weapon.Value)
end
local success, err = pcall(function()
DataStore:SetAsync(Player.UserId, save)
end)
if not success then
warn("Could not save data.")
end
I’m trying to figure out how to load the StringValues inside of this folder called gunFolder which is inside of the Player. So far I figured out how to save the StringValues by looping through the folder and inserting them into a table. However, I’m not sure how to load the saved data? How would I loop through playerStats without knowing how many StringValues were saved?
Maybe use a dictionary, save the name of the instance and set it to its value, something like:
local save = {}
save[Player.leaderstats.Credits.Name] = Player.leaderstats.Credits.Value
Repeat it for all the values (you can do it easily with :GetChildren()
), then when you load the data simply find the name of the instance and set its value to the value found in the dictionary.
Currency.Value = save["Credits"]
This might not be the best solution but that’s how I’d do it.
Hey thanks for the reply! I’m not sure if you were talking about the values inside of the gunFolder because my problem right now is just figuring out how to grab just the StringValues from the DataStore and not the other leaderstats.
The gunFolder would like this for example:
gunFolder = “USP”, “AK-47”, “M1911”, “AWP”
I’m trying to figure out how to grab those values without knowing how many values are in the folder and loading them back into the folder.
1 Like
Oh well you can use the same method I’ve told you
save = {};
save["USP"] = ValueHere;
This won’t require you to know the total number of values inside of it, and to check if there isn’t a value for that specific weapon you can just do if data[Weapon.Name] ~= nil then
.
1 Like
I’m pretty terrible with tables and datastores could you provide me with some psuedocode on how I would load the weapon data into the folder? For some reason it keeps loading the other leaderstats values when I use local playerStats = DataStore:GetAsync(Player.UserId)
This is the loading part that I’m having issues with:
if playerStats ~= nil then
Currency.Value = playerStats[1]
Kills.Value = playerStats[2]
Level.Value = playerStats[3]
Exp.Value = playerStats[4]
ExpNeeded.Value = playerStats[5]
--Loading the weapons section
print(Player.Name.. "'s data has been loaded!")
else
ExpNeeded.Value = 1000
print(Player.Name.. " has no data!")
end
To load the data, you would loop through all the weapons you saved. The problem is you don’t have the weapons separated from the leaderstats so looping through the weapons would loop through the leaderstats. You can fix this by having a weapons table inside of the player data which stores all the weapons.
Also, instead of using table.insert, you should use a dictionary. Currently you load currency with playerStats[1], kills with playerStats[2], etc. These are called magic numbers, which can lead to bugs. A better way is to insert using something like save["Credits"] = Credits.Value
and when loading you just do Credits.Value = playerStats["Credits"]
instead of relying on the order of which the stats are saved