Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Datastore, how could I manage, save and load more data without Instance.new() [?]

#Goal:# My Current goal it to save an inventory. The inventory is those that contain an ImageLabel with an image and a Text label to display the quantity of that item.

#Problem:# My problem is that currently when I save things such as "money" I use Instance.new() to hold the value and save/load from that. But say I wanted to make a system where it has a quantity of an item? I don't want to create more Instance.new() and based on how many different items there are there will be a lot of IntValues in a folder. So I want to learn a different way of doing this.

#Script (This is a section):# This is a ServerScript in ServerScriptService

game.Players.PlayerAdded:Connect(function(plr)
	local Stats = Instance.new('Folder')
	Stats.Name = 'Stats'
	Stats.Parent = Player

	local ExampleStat = Instance.new('IntValue')
	ExampleStat.Name = 'ExampleStat'
	ExampleStat.Value = 0
	ExampleStat.Parent = Stats
end)

local function Print(message) -- Ignore my for fun stuff
    if PRINT_OUTPUT then print(message) end
end

local function SaveData(player)
    if player.UserId < 0 then return end
    player:WaitForChild("Stats")
    wait()
    local Stats = {}
    for i, stat in pairs(player.Stats:GetChildren()) do
        table.insert(Stats, {stat.Name, stat.Value})
    end
    leaderboardData:SetAsync(player.UserId, Stats)
	Print('Printing Report From: '..script.Name)
    Print('Report: '..player.Name.." ["..player.UserId.."]".."'s data was Saved!")
end

local function LoadData(player)
    if player.UserId < 0 then return end
	wait()
    player:WaitForChild("Stats")
    wait()
    local leaderboardStats = leaderboardData:GetAsync(player.UserId)
	print(leaderboardStats)
    for i, stat in pairs(leaderboardStats) do
        local currentStat = player.Stats:FindFirstChild(stat[1])
        if not currentStat then return end
        currentStat.Value = stat[2]
    end
	Print('Printing Report From: '..script.Name)
    Print('Report: '..player.Name.." ["..player.UserId.."]".."'s data was Loaded!")
end

players.PlayerAdded:Connect(LoadData)
players.PlayerRemoving:Connect(SaveData)

if SAFE_SAVE then
    game.OnClose = function()
        for i, player in pairs(players:GetChildren()) do
            SaveData(player)
        end
        wait(1)
    end
end

while AUTO_SAVE do
    wait(TIME_BETWEEN_SAVES)
    for i, player in pairs(players:GetChildren()) do
        SaveData(player)
    end
end

Chances are I will have to deal with tables. Basically, I don't want to Instance.new() spam. Or else If a game had 50 items I would need 50 IntValues. and 50 Instance.new()'s

So how could I do this? Can someone please provide a template or an example? Thank you for reading and I appreciate any helpful feedback.

6 comments

0

Save the .Value of the object to a DataStore ... Please ask specific questions.

0

How do you save that?

0

Is the name of "NPCItems" that you have to find the value of constant? Or does it change?

0

IT Changes

0

I believe that you can't really make an changes to it, coming from someone who is intermediate at scripting. Someone else might say that it could be improved, but it seems fine to me.

0

I see

1 answer

✓ Accepted answer

To do this, you would use a table full of the values of the values. Sounds weird.

Here is an example:

local value1 = Instance.new("IntValue")
value1.Value = 10

local value2 = Instance.new("StringValue")
value2.Value = "String"

local value3 = Instance.new("BoolValue")
value3.Value = true

local valuesInTable = {value1.Value, value2.Value, value3.Value}

datastore:SetAsync(key, valuesInTable)

The code above would store all the values inside the values in a table, and then save that table in the datastore.

However, if you have more than 3 values (which I assume you probably do), you could use a for loop.

local statValues = statsFolder:GetChildren() -- Assuming you have a folder full of values somewhere.

local valuesToSave = {} -- Empty table
for index, statvalue in pairs(statValues) do
	valuesToSave[index] = statvalue.Value -- The element of that index will have the value of that statvalue.
end

datastore:SetAsync(key, valuesToSave)

This discussion is preserved as it was. New answers, comments, and votes are closed.