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

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

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!

6 comments

0

Worst security ever. Someone could just go fire the remote event and get rich.

0

y dis site no support emojis D:

0

that's what i was thinking.I'll have to change it later. My problem stands though.

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.

0

The ModuleScript function (last snippet of code), the line that I commented on

0

"Player_Player.UserId"["Resources"] does not work. You are trying to treat a string like a dictionary.

1 answer

✓ Accepted answer

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

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