How to do session locking

so profile service is known for having session locking but i was wondering how hard it would be to just write the code yourself

  • give any links on the docs for how to do this

my attempt at session locking using GenerateUUID i read that this is better than just using JobId

function Main.INIT(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local HttpService = game:GetService("HttpService")

	--// Cash
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"

	local success, data = pcall(function() return playerData:GetAsync(player.UserId.."-Cash") end)
	if not success or type(data) ~= "table" then
		warn("Failed to get data for player " .. player.UserId)
		data = {Value = 0, SessionJobId = nil, SessionLockTime = os.time()}
	end

	local JobId = HttpService:GenerateGUID()

	if data.SessionJobId == nil or data.SessionJobId == JobId then
		data.SessionJobId = JobId
		Cash.Value = data.Value
	else
		warn("Data is currently locked by another session.")
		Cash.Value = 0
	end
	Cash.Parent = leaderstats

	--// Rebirths
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"

	local success, data = pcall(function() return playerData:GetAsync(player.UserId.."-Rebirths") end)
	if not success or type(data) ~= "table" then
		warn("Failed to get data for player " .. player.UserId)
		data = {Value = 0, SessionJobId = nil, SessionLockTime = os.time()}
	else
		print("Success")
	end

	if data.SessionJobId == nil or data.SessionJobId == JobId then
		data.SessionJobId = JobId
		Rebirths.Value = data.Value
	else
		warn("Data is currently locked by another session.")
		Rebirths.Value = 0
	end
	Rebirths.Parent = leaderstats

	while task.wait(0.1) do
		Cash.Value += 1
	end	
end
1 Like

I recommend checking this part of @GEILER123456’s data store tutorial

1 Like