104: Cannot store Dictionary in data store

Server Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local DefaultKeybinds = require(ReplicatedStorage.ModuleScripts.Shared.DefaultKeybinds)

local dataStore = DataStoreService:GetDataStore("Keybinds")

local function onPlayerAdded(player: Player)
	local success, result = pcall(function()
		dataStore:GetAsync(player.UserId)
	end)
	if not success or not result then
		warn("Failed to retrieve client keybinds data; Resetting to default keybinds.")
		dataStore:SetAsync(player.UserId, DefaultKeybinds)
		print(dataStore:GetAsync(player.UserId))
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)

Default Keybinds ModuleScript

local DEFAULT_KEYBINDS = {
	["Pass"] = {
		["Pass"] = Enum.UserInputType.MouseButton1
	}
}

return table.freeze(DEFAULT_KEYBINDS)

So in the data store script, I am basically just printing to see if it actually set the dataStore properly, but then it does this error?

DataStoreService: CantStoreValue: Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters. API: SetAsync, Data Store: Keybinds

You can’t store anything that’s an instance/userdata class. An enum isn’t directly storable, but you can store the enum’s value.

2 Likes

The problem is that Enum.UserInputType.MouseButton1 is not a valid value as it cannot be represented in JSON format.

You could use:

["Pass"] = "Enum.UserInputType.MouseButton1"

or just the name/value of the Enum:

["Pass"] = "MouseButton1" -- Name
["Pass"] = "0" -- Value

Personally I recommend you to use the name method.

1 Like

If I do this method how do I check if its an Enum.Keycode or Enum.UserInputType

This is more or less how it should look

ServerScript:


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local DefaultKeybinds = require(ReplicatedStorage.ModuleScripts.Shared.DefaultKeybinds)

local dataStore = DataStoreService:GetDataStore("Keybinds")

local function onPlayerAdded(player: Player)
	local success, result = pcall(function()
		return dataStore:GetAsync(player.UserId)
	end)
	
	if not success or not result then
		warn("Failed to retrieve client keybinds data; Resetting to default keybinds.")
		
		local convertedKeybinds = table.clone(DefaultKeybinds)
		for action, keybind in pairs(convertedKeybinds) do
			for key, inputKey in pairs(keybind) do

				convertedKeybinds[action][key] = tostring(inputKey)
			end
		end
		
		dataStore:SetAsync(player.UserId, convertedKeybinds)
		print(dataStore:GetAsync(player.UserId))
	else

		local playerKeybinds = result
		for action, keybind in pairs(playerKeybinds) do
			for key, inputKey in pairs(keybind) do

				playerKeybinds[action][key] = Enum.UserInputType[inputKey]
			end
		end
		
		print("Loaded keybinds for player:", player.UserId, playerKeybinds)
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)


ModuleScript:


local DEFAULT_KEYBINDS = {
	["Pass"] = {
		["Pass"] = "MouseButton1" 
	}
}

return table.freeze(DEFAULT_KEYBINDS)

For simplicity, just use the first option.
This one:

["Pass"] = "Enum.UserInputType.MouseButton1"
1 Like

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