Datastore Question

Hello! I’m not too good with datastores, but I was wondering how I would add/remove an item that is being saved.

For example, if a new player joins, I would give them some new data such as:

local tableOfData = {
    ["Money"] = 10,
    ["Level"] = 1,
    ["Rank"] = "Noob"
}

However, if they already have data, I would just load it in. I was wondering if there was a way to add a new value to the table for players who already have data saved. I’ve tried something like this:

local tableOfData = {
    ["Money"] = 10,
    ["Level"] = 1,
    ["Rank"] = "Noob"
}

if not table.find(tableOfData[player.UserId], "Diamonds") then
    table.insert(tableOfData[player.UserId], "Diamonds")
    tableOfData[player.UserId].Diamonds = 0
end

But this doesn’t work. I’d like it to be the same the other values. The table should look like this:

local tableOfData = {
    ["Money"] = 10,
    ["Level"] = 1,
    ["Rank"] = "Noob"
    ["Diamonds"] = 0
}

Instead, it looks like this:

local tableOfData = {
    [1] = "Diamonds"
    ["Money"] = 10,
    ["Level"] = 1,
    ["Rank"] = "Noob"
}

I’m not sure how to add it so that the new value is in the same format as the others. Sorry if this question is a bit confusing, but if anyone could help, that would be great! Thanks.

Check if the Player’s Current Data doesnt have this new by Iterating through it:

for index,value in Data do -- Iterates through Starter Data
    if Sessions[Player.UserId][index] == nil then -- if Data doesnt Exist
        Sessions[Player.UserId][index] = value -- adds Data
    end
end

i think you can use table.find() for this however

1 Like

I can’t believe I didn’t think about that. I’m tired, but thank you!

The reason why its doing [1] = "Diamonds" is that the table.insert() function adds it as if its an array, aka {'a', 'b', 'c'} which is the same thing as a {[1] = 'a', [2] = 'b', [3] = 'c'}, so doing table.insert() into the {'a', 'b', 'c'}, its just creates a new index called 4 and sets it to the value that you passed into the table.insert(). But we are working with dicts (Dictionaries which has a key index), so you have to set the index name aka table[indexName] = value. For example in your case it would be tableOfData['Diamonds'] = 0

1 Like

I figured that would be the reason. Thank you for the explanation though!

your welcome, sorry if i was late to the party lol

1 Like

I think this could work:

a = {}

function InsertAndApply(t: {}, value: any, Key: string?)
	local A = table.insert(t, value)
	local index = table.find(t, value)
	
	local CurrentValue
	if index then
		CurrentValue = t[index]
		t[Key] = CurrentValue
    else
        return
	end
	table.remove(t, index)
	
end

InsertAndApply(a, "Hello", "Sup")
print(a)

Could be simplified tho

But there wouldn’t be any use of that since you are just complicating the a["Hello"] = "sup"

1 Like

Still, at least it works

Summary

lol

1 Like

ah yes complicating things that can be redone in one single line. Every developer be like

lol

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.