Help modifying dictionary values in a datastore

Hello, for the third time today!

Still on-topic from my admin panel work, I’m working on a ban function so I can moderate stuff. Currently, things work well, but for the life of me I cannot figure out how to modify a value to help my server detect whether a player is banned when joining a server.

For clarification, here’s what the average player’s datastore looks like:
image
My goal right now it a small step: change IsBanned from false to true.

I have literally no clue where to start so any help would still be hugely appreciated.

You’re downloading and uploading data at some point in your codebase. You can store “IsBanned” in some instance or table just as you already are. Modifying it is as simple as toggling that value, and your codebase will take care of saving the data. If you want to target a player who’s offline, use GlobalDataStore:UpdateAsync with their key to modify the table.

Ultimately, there is no need for you to add a ban flag. Roblox introduced a native ban API through the Players service:

Players:BanAsync
Players:UnbanAsync
Players:GetBanHistoryAsync

This API extends to your creator dashboard, and can be externally operated through web-servers

I recommend having a datastore just for banned players. The only thing the server does is get the player’s data to check if they are banned when they log in.

This is because the safest way to do this is with a shared datastore using UpdateAsync, but some data may end up being overwritten, causing a rollback, which is extremely bad.

UpdateAsync is designed to act as a first-line defence against data corruption; any adverse overwrites that may occur are entirely due to ineffective implementation. The ban API that Roblox has provided acts as a single data-store, so if that’s a pro to you, then all the more reason to use it

The server does more than check to see if a player is banned, it was just the only example I listed.

The reason I’m using this arbitrary method to gather player information and not just setting datastore stuff using my existing profile helper is because I don’t have access to a player instance, it’s either the player’s username or their userId, which would be impossible to get a player instance with just their userId

No player instance is required. The data you’re manipulating is not stored in runtime memory, but a database. That data is linked to a key, which is most likely that player’s user ID. You can use their user ID directly, or if you have a username, retrieve the associated user ID via Players:GetUserIdFromNameAsync

image
Seen here, the key stored in my main database is ‘Player_’ followed by the player’s userId. I tried modifying the key’s value via :SetAsync(), however it ended up wiping everything under ‘Player_userid’. I’m not experienced at all with manipulating dictionary information like this, so if you could actually provide an example of how I could manipulate one of these values, that’d be awesome!

My ProfileHelper has functions to manipulate values, but those modules are stored in ServerScriptService, and afaik, cannot be accessed by client scripts, so I’m trying to figure out alternatives to modify these values while staying in client scripts.

I have a ban system like you are doing and it has a custom duration in hours, days, etc.

I did some testing, so I recommend that you use a datastore separate from the player data to avoid ANY possibility of problems.

If you want to make any changes to this data, just do this:

In this specific case, you could use UpdateAsync, but if you do some manipulation between the server and the data obtained, this will work.

What you need to learn is how to manipulate tables, but I can tell you right away that it’s quite simple. It may seem a bit more challenging at first, but you’ll learn quickly.

Edit1: I’ve verified that the data is inside a table called Data, so you’ll need to do this (I changed the naming just to make it easier)

PlayerData = DS:GetAsync(PlayerID)
PlayerData[“Data”][“IsBanned”] = true
DS:SetAsync(PlayerData)

image
Tried to set it up as you did, :SetAsync() seems to be missing a second argument tho

Sorry, I simplified the code and forgot to include all the arguments.

SetAsync needs the data and the key, in this case the PlayerID.

It’s fine, I assumed you made a mistake and tested it with the added key, however it ended up corrupting the key
image
I have no idea how it ended up overwriting the Data dictionary, but something happened.
On the upside, it did end up modifying the ban value.

The problem was because you made the variable as
PlayerData.Data
If you had made it just as PlayerData, it wouldn’t have corrupted.

Fix it like this:

OldData = DS:GetAsync(key)
FixedData = Data = {
  OldData
}

DS:SetAsync(FixedData, key)

Not entirely sure what you’re doing here, but I gave it a shot anyways.

First tried just getting rid of the .Data thing I had going in the line you mentioned, which didn’t corrupt the datastore, but created a new value that wasn’t under ‘Data’.

Moving onto what you suggested here, I’m running into syntax errors and I’m not sure if it’s the way you wrote it or an oversight on my end.

So what I mean is, manipulate the root data, so any changes you make, do it on top of the root, because when you save, you will have all the dictionaries intact.

If PlayerData has a “Data” dictionary, do not use the main variable like:

local PlayerData = DS:GetAsync(key).Data
Data[“IsBanned”] = true

Instead use:

local PlayerData = DS:GetAsync(key)
Data[“Data”][“IsBanned”] = true

image
Tried doing that, still no results.

Edit: I’m getting pretty tired and quite frankly sick of trying to debug this for hours on end so I’m gonna head out for the night and I’ll pick this back up tomorrow. Feel free to still throw suggestions in here and I’ll work on implementing those.

Again, use UpdateAsync. The dictionary you’ve presented us does not organize its data under a “Data” key, so I’m not sure why that’s included in your code. It should look something like:

store:UpdateAsync(key, function(data)
    if not data then
        warn("No data associated with key: " .. key)

        return
    end

    data.IsBanned = true

    return data
end)

image
Tried that, noticed a similar trend of it creating a new value under the key rather than updating the value in Data.

I would opt for a method like this since it seems easiest, but that would mean I’d have to edit the ProfileService to reflect this rather than updating the ProfileTemplate.

Your schema is so confusing. Please be sure to share a more comprehensive version of it first. Try:

store:UpdateAsync(key, function(data)
    if not data then
        warn("No data associated with key: " .. key)

        return
    end

    data.Data.IsBanned = true

    return data
end)