EasyProfile - A Simple Way to Store Player Data

Everything looks great and very easy to understand but I have a quick question, is there a way to easily check if the players data has been changed? I see that there is __index but not __newindex so I’m not entirely sure. Another question is how do I override the data if they’re offline? Like how do I change it? I saw it in the documentation and it didn’t mention anything about how to do it.

There’s no way to check if it has been changed; to change it when they are offline use the function returned from :GetProfileAsync() (in a tuple #2) which when run will override it with the edited table from tuple #1

Also if you ever want to add it to yours, this is how you can detect value change (does not detect tables)

local function PlayerAdded(player : Player)
	ServerDataStore:LoadProfileAsync(player, true):After(function(success, playerProfile)
		if not success then
			player:Kick(DataErrorMessage)
			return
		end
		
		--/ Create Leaderstats
		playerProfile:CreateProfileLeaderstats(player, {"Coins"})
		
		local profile = playerProfile.Profile
		
		local dummy = {
			Data = {}
		}
		local callbacks = {}
		
		--/ Value Changing
		dummy.Data.__index = function(_,index)
			return profile.Data[index]
		end
		dummy.__index = function(_,index)
			if (index ~= "Data") then
				return profile[index]
			end
		end
		
		dummy.Data.__newindex = function(_,index,value)
			profile.Data[index] = value

			local callback = callbacks[index]
			if not callback or typeof(callback) ~= "table" then return end

			for _, callback in callback do
				coroutine.wrap(callback)(value)
			end
		end
		dummy.__newindex = function(_,index,value)
			profile[index] = value
		end

		setmetatable(dummy.Data,dummy.Data)
		setmetatable(dummy,dummy)
		
		function dummy:OnDataValueChanged(index, callback)
			if (not callbacks[index]) then
				callbacks[index] = {}
			end
			table.insert(callbacks[index],callback)

			local Connection = {}
			function Connection:Disconnect()
				table.remove(callbacks[index],table.find(callbacks[index],callback))
			end
			return Connection
		end
		
		dummy:OnDataValueChanged("Coins", function(new)
			print("Coins:", new, "Data:", playerProfile:GetProfileData().Coins)
		end)
		
		LoadedProfiles[player] = dummy
	end)
end

So this will change their data? I’m not sure what you exactly mean by tuple #2, etc.

ServerDataStore:GetProfileAsync(1):After(function(success, data)
	data.Cash += 100
end)

My bad, it would be success, data, overwrite in that order where overwrite is a function you can run.

1 Like