How to store button position values in profileservice?

Hi,

I am having problems trying to store button position values… How do I go around storing a dictionary in a data store?

local DataManager = {}

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local ProfileService = require(script.ProfileService)

local DataTable = {
	ButtonPositions = {
		[1] = UDim2.new(0,0,0),
		[2] = UDim2.new(0,0,0),
		[3] = UDim2.new(0,0,0),
		[4] = UDim2.new(0,0,0)
	};
}

local ProfileStore = ProfileService.GetProfileStore("Test1", DataTable)	
local Profiles = {}

local function HandleLockedUpdate(GlobalUpdates, Update)
	local id = Update[1] 
	local data = Update[2]

	GlobalUpdates:ClearLockedUpdate(id)
end

local function OnPlayerAdded(player)
	local profile = ProfileStore:LoadProfileAsync(
		"Player_"..player.UserId,
		"ForceLoad"
	)	
	
	if profile then
		profile:AddUserId(player.UserId)
		profile:Reconcile() 
	
		profile:ListenToRelease(function() 
			Profiles[player] = nil 
			player:Kick()
		end)

		if player:IsDescendantOf(Players) then 
			Profiles[player] = profile

			local globalUpdates = profile.GlobalUpdates

			for index, update in pairs(globalUpdates:GetActiveUpdates())do
				globalUpdates:LockActiveUpdate(update[1])
			end

			for index, update in pairs(globalUpdates:GetLockedUpdates())do
				HandleLockedUpdate(globalUpdates, update)
			end

			globalUpdates:ListenToNewActiveUpdate(function(id, data)
				globalUpdates:LockActiveUpdate(id)
			end)

			globalUpdates:ListenToNewLockedUpdate(function(id, data)
				HandleLockedUpdate(globalUpdates, {id, data})
			end)

		else
			profile:Release() 
		end
	else
		player:Kick("Data loading failed")
	end
end

local function OnPlayerRemoving(player)
	local profile = Profiles[player]
	
	if profile then
		profile:Save()
		profile:Release()
	end
end

-- Public Methods

function DataManager:GetData(player)
	local profile = Profiles[player]

	if profile then
		return profile.Data
	else
		return nil
	end
end

function DataManager:GetProfileStore()
	return ProfileStore
end

function DataManager:UpdateData(player, key, data)
	local profile = Profiles[player]

	if key == "" then
		if profile.Data[key] and profile.Data[key] > data then
			return
		end
	end

	profile.Data[key] = data
	print("UpdateData:" ,player, key, data)
end

function DataManager:SaveData(player)
	Profiles[player]:Save()
end

function DataManager:ClearData(player) 
	if RunService:IsStudio() and RunService:IsServer() then
		local profile = Profiles[player] or ProfileStore:LoadProfileAsync(
			"Player_"..player.UserId,
			"ForceLoad"
		)
		profile["Data"] = DataTable
		player:Kick("Data Cleared")
	end
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)

return DataManager
1 Like

I figured out I have to seralize the udim, but i’m still trying to figure out how…

I found this though: RBLXSerialize - a easy to use and really cool all-in-one Roblox Serializer

Yeah that resource seems pretty good, but if you want a simple solution you can just store a table with the UDim2 values (x and y scale and offset)
and then when loading the profile just construct a new UDim2 out of the stored values

local LoadedValues = {0,0,0,0}
Udim2.new(table.unpack(LoadedValues))

I’m not sure how I would use this in your case. Can you help me out?

Is this what you want me to do?

ButtonPositions = {
	[1] = UDim2.new(-0.4169, 0, 0.715, 0),
	[2] = UDim2.new(-0.165, 0, -0.165, 0),
	[3] = UDim2.new(0.715, 0, -0.4169, 0),
};

table.unpack(ButtonPositions)

This seemed to have no errors though:

local Serializer = require(game:GetService("ReplicatedStorage").Sources.RBLXSerialize)

local DataTable = {
	ButtonPositions = {
		[1] = Serializer.Encode(UDim2.new(-0.4169, 0, 0.715, 0),true),
		[2] = Serializer.Encode(UDim2.new(-0.165, 0, -0.165, 0),true),
		[3] = Serializer.Encode(UDim2.new(0.715, 0, -0.4169, 0),true)
	};
}

I’ll see if I can decode it.

No, i meant storing just the property values instead:

local butPos = UDim2.new(0,0,0,0)

local DataTable = {
	ButtonPositions = {
		[1] = {
            butPos.X.Scale,
            butPos.Y.Scale,
            butPos.X.Offset,
            butPos.Y.Offset
        },
    --  [2] = {...}
	};
}

--save the previous table somewhere
--then when you want to use it, create an udim value from it:

local newUdim = UDim2.new(table.unpack(DataTable.ButtonPositions[1]));
1 Like

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