Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[SOLVED] How would I change only one value from a table using data store?

Asked by
LawlR 182
5 years ago
Edited 5 years ago

My datastore consists of 4 values, three being int values and 1 a bool.

Lets say that the bool value is currently true, but I want to change it to false.

How would I do this without changing any of the int values?

DataStore:SetAsync(key, {1 ,2, 3, false})

This works, however it also changes the int values. Is there a way to get the player's current values or somehow not change the int values?

I know you can use GetAsync, but how would I implement that into the SetAsync, if even possible?

Edit: This is in case I want to change a player's value from inside studio at any time. I do not need help saving their value when they leave/periodically.

Edit2:

I figured it out.

local First = (DataStore:GetAsync(key)[1])
local Second = (DataStore:GetAsync(key)[2])
local Third = (DataStore:GetAsync(key)[3])
local Fourth = (DataStore:GetAsync(key)[4])
DataStore:SetAsync(key, {First ,Second, Third, false})

Whats pretty cool is that you can do First + 2 or whatever to give someone a little bonus.

1 answer

Log in to vote
0
Answered by
jaschutte 324 Moderation Voter
5 years ago
Edited 5 years ago

Is this what you mean?:

DataStore:SetAsync(key, {1 ,2, 3, false})
local tab = DataStore:GetAsync(key)
function changeKey(t, key, newKey, changeAll)
    for k,v in pairs(t) do
        if v == key then
            v = newKey
            if not changeAll then
                break
            end
        end
    end
    return t
end
local newTable = changeKey(tab, false, true, false)
DataStore:SetAsync(newTable)

--the actual code is /\
--[[
    Description of function:
        [table] t = the table the key is in
        [AnyValue] key = the key you want to replace
        [AnyValue] newKey = the replacer for the key
        [OPTIONAL][Bool] changeAll = change all of the keys in the table that are the same as argument 2 (key). Defaults to nil, will stop after finding the first key.
--]]

This should work. Don't worry about the optional argument, in your case you won't need it.

0
This seems like it works (haven't tested it though), however the solution I found I find easier to understand and simpler. LawlR 182 — 5y
Ad

Answer this question