Profile Service How To Win With Tables?

what do i mean?

well i have a table here in a profile service module

	Slot1 = {
		["Difficulty"] = "None",
		["IslandName"] = "None",
		["Multiplayer"] = "Off",
		["TimeCreated"] = "None",
		["IslandCreated"] = "None",
	}

but i cant figure out how to change the text of any of the objects within, example: "[“Difficulty”] = “Easy” stuff like that

the 2 functions that can change something within profile service

function SlotSettings:Set(player, key, value)
	local profile = getProfile(player)
	if profile.Data[key] == nil then
		error(string.format("Data doesn't exist for key: %s", key))
	end
	assert(type(profile.Data[key]) == type(value), string.format("Value Type Incorrect for key: %s", key))

	profile.Data[key] = value
	return profile.Data[key]
end

function SlotSettings:Update(player, key, callback)
	local profile = getProfile(player)

	local oldData = self:Get(player, key)
	local newData = callback(oldData)

	self:Set(player, key, newData)
end

my attempt on making it work

	local Slot1Table = SlotSettings:Get(Player, "Slot1")
	
	local Slot1 = SlotSettings:Get(Player, "Slot1")
	SlotSettings:Update(Player, Slot1["Difficulty"], function(CurrentDifficulty)
		return CurrentDifficulty == "Easy"
	end)

doing this error

ServerScriptService.Modules.SlotSettings:100: Data doesn't exist for key: None

i had a post like this before, but only was able to fix one of the more annoying ones, but this brings a new one. I hope you are able to help

I will be honest, I was looking at your other PorfileService post that you marked as solved. It wasn’t solved. Bracketing keys in tables is only required when you have a space inside the key. ["Island Name"] where “IslandName” is fine.

The problem you are having is you are sending Slot1["Difficulty] as an argument. You are basically telling the script to use the Value of Slot1[Difficulty] rather than the Path through the table. Changing this will cause other issues as you update your code correctly.

I do use Profile Service as well, and have for a long time, this is how I handle most of my Get/Set functions for my profile.Data, do note sometimes I use situational one-offs.

Rather than completely change your code for you, as you’re probably going to have to change a bit, let me provide my code that I use, and point you in the correct direction. If you have any other questions about what the code provided does, or want a better point of direction, feel free to DM me on the DevForums.

-- Loops through the given table ( I usually pass the entire profile.Data table)
-- and returns the given Value of Key (if found). Good for Profile:GetData() uses
local function TableSearchValue(SearchTable, SearchValue)
	for Key, Value in pairs(SearchTable) do
		if Key == SearchValue then
			return Value
		elseif type(Value) == "table" then
			local result = TableSearchValue(Value, SearchValue)
			if result then
				return result
			end
		end
	end
	return nil
end

-- Loops through the given table ( I usually pass the entire profile.Data table)
-- and returns the path to be able to update. In your case, use such as:
-- local Path = TableSearchPath(profile.Data, "Difficulty") would return profile.Data.Slot1
-- where you then do Path["Difficulty"] = true (Updating your Difficulty value)
local function TableSearchPath(SearchTable, SearchValue, Path)
	Path = Path or SearchTable
	for Key, Value in pairs(SearchTable) do
		local currentPath = Path
		if Key == SearchValue then
			return currentPath
		elseif type(Value) == "table" then
			local result = TableSearchPath(Value, SearchValue, currentPath[Key])
			if result then
				return result
			end
		end
	end
	return nil
end

-- Returns the given Value at Key
function ProfileModule:GetProfileData(player, DataName)
	local profile = self.Profiles[player]
	if DataName then
		return TableSearchValue(profile.Data, DataName)
	end
	return self.Profiles[player]
end

-- Both :UpdateProfileData and :SetProfileData both change the values of DataName
-- to DataValue but I use them for different things, which is why there are two.
function ProfileModule:UpdateProfileData(player, DataName, DataValue)
	local profile = self.Profiles[player]
	if DataName and DataValue then
		local profilePath = TableSearchPath(profile.Data, DataName)
		if type(DataValue) == "number" then
			profilePath[DataName] += DataValue
		else
			profilePath[DataName] = DataValue
		end
	end

	if profile.Data.TrackerData["Total".. DataName] and DataValue > 0 then
		self:UpdateProfileData(player, "Total".. DataName, DataValue)
	end

	self:CheckUpdate(player, DataName)
end

function ProfileModule:SetProfileData(player, DataName, DataValue)
	local profile = self.Profiles[player]
	if DataName and DataValue then
		local profilePath = TableSearchPath(profile.Data, DataName)
		profilePath[DataName] = DataValue
	end

	self:CheckUpdate(player, DataName)
end
1 Like

this was confusing for me too when i first looked at profile service

youre supposed to create a table in a module script (so you can access from multiple scripts), and then use profile service to save that table, but you don’t have to save the data every time to update the table

I believe he is already doing that considering his local profile = getProfile(player) code which is different than the more common local profile = Profiles[player] like in my code, but for each their own.

The issue the OP is running into is SlotSettings:Update(Player, Slot1["Difficulty"], function(CurrentDifficulty) where they pass the value at Slot1["Difficulty"] rather than the Key to Set to/Get from (:Get() is not shown in this code but was shown in his other post I mentioned to).

But his simple seeming change would cause an issue as Difficulty is nested inside profile.data at profile.Data.Slot1.Difficulty and his current code would still not find the key at the error() or assert() lines since he would be searching for profile.Data.Difficulty and bypassing Slot1.

thanks for the reply! Ill look into what you said, and for my last post. It did seem to fix my error, but the only problem was i didnt know how to change the value

also could you tell me more on

profile.Data

Im new to profile service, and i dont really know how i can access this

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