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

How do I update a single key from a dictionary inside of a Player's DataStore key?

Asked by 6 years ago
Edited 6 years ago

This is the format I'm using for the dictionary to SetAsync()

local PlayerData = {
    ["Models"] = {

    };
    ["Resources"] = {
        ["Money"] = 1; -- I want to update one of these 
        ["Population"] = 12;
        ["Wood"] = 24;
        ["Stone"] = 64;
        ["Electric"] = 14;
        ["Water"] = 163;
    }
}

This is the script I wrote to tell the server that the player's Data was changed

local Event = game.ReplicatedStorage.remoteevents.SaveData
wait()
for i, resource in pairs(game.Players.LocalPlayer:WaitForChild("Resources"):GetChildren()) do
    resource:GetPropertyChangedSignal("Value"):Connect(function()
        Event:FireServer(resource, resource.Value)
    end)
end

Corresponding remote event script:

game.ReplicatedStorage.remoteevents.SaveData.OnServerEvent:Connect(function(player, resource, value)
    player.Resources[resource.Name].Value = value
    DataStore.UpdateResource(player, resource.Name, value) -- this is my own function inside of a modulescript
end)

Lastly, here is my ModuleScript function:

function DataStore.UpdateResource(Player, Resource, Value)
    local DataStoreService = game:GetService("DataStoreService")
    local Data = DataStoreService:GetDataStore("PlayerData"):GetAsync("Player_" .. Player.UserId)
    DataStoreService:GetDataStore("PlayerData"):SetAsync(("Player_" .. Player.UserId)["Resources"][Resource], Value) -- Everything works fine up until here
end

error: 15:05:37.513 - ServerScriptService.SaveDict.SaveData:15: attempt to index field 'Resources' (a nil value) How do I access a single key inside of the dictionary "Resources" Which is inside of the dictionary "Player_26246132"? any help is greatly appreciated!

0
Worst security ever. Someone could just go fire the remote event and get rich. hiimgoodpack 2009 — 6y
0
y dis site no support emojis D: hiimgoodpack 2009 — 6y
0
that's what i was thinking.I'll have to change it later. My problem stands though. creeperhunter76 554 — 6y
0
Can you say what exact script the error is occurring on? None of those snippets of code have 15 lines so there is no way to tell which one is the problem. TheDeadlyPanther 2460 — 6y
View all comments (2 more)
0
The ModuleScript function (last snippet of code), the line that I commented on creeperhunter76 554 — 6y
0
"Player_Player.UserId"["Resources"] does not work. You are trying to treat a string like a dictionary. TheDeadlyPanther 2460 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Alright, I see your problem.

The value saved in each player is a dictionary - but the key is not (what you are trying to do currently is edit the specific key in the key).

What you do is save the entire dictionary after you have edited it:

local NewData = DataStoreService:GetDataStore("PlayerData"):GetAsync("Player_" .. Player.UserId)
NewData["Resources"][Resource] = Value

DataStoreService:GetDataStore("PlayerData"):SetAsync("Player_" .. Player.UserId, NewData)

Note: I believe saving dictionaries doesn't work that well. You'll need to convert it to another format first - people usually use JSON.

Saving:

NewData = HttpService:JSONEncode(NewData)
DataStoreService:GetDataStore("PlayerData"):SetAsync("Player_" .. Player.UserId,NewData)

Loading:

local LoadedData = DataStoreService:GetDataStore("PlayerData"):GetAsync("Player_" .. Player.UserId)
LoadedData = HttpService:JSONDecode(LoadedData)

Hope I helped!

~TDP

Ad

Answer this question