ProfileService Handler

unlisted ⁨1⁩ ⁨file⁩ 2024-01-11 10:40:39 UTC

Handler.lua

Raw
local GetData = game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("GetDataValue")
local Players = game:GetService("Players")
local PS = require(game:GetService("ServerScriptService"):WaitForChild("ProfileService"))
local Statistics = require(game:GetService("ServerScriptService"):WaitForChild("DataModules"):WaitForChild("Statistics"))
local Settings = require(game:GetService("ServerScriptService"):WaitForChild("DataModules"):WaitForChild("Settings"))
local Template = require(script.Parent:WaitForChild("Template"))

local ProfileStore = PS.GetProfileStore("PlayerData", Template)
local Profiles = {}

function getDataValue(Player, Value)
	local Profile = Profiles[Player]
	if not Profile then return end
	
	return Profile.Data[Value]
end

GetData.OnServerInvoke = getDataValue

local function PlayerAdded(Player: Player)
	local Profile = ProfileStore:LoadProfileAsync(tostring(Player.UserId))
	if Profile == nil then
		Player:Kick("Data issue, try again shortly. If issue persists, contact us")
		return
	end
	
	Profile:AddUserId(Player.UserId)
	Profile:Reconcile()
	Profile:ListenToRelease(function()
		Profiles[Player] = nil
		Player:Kick("Data issue, try again shortly. If issue persists, contact us")
	end)
	if Player:IsDescendantOf(Players) == true then
		Profiles[Player] = Profile
	else
		Profile:Release()
	end
	
	wait(1)
	Player.PlayerGui.Main.SpeedDisplay.TextLabel.Text = tostring(Profile.Data.Money)
end

for _, player in ipairs(Players:GetPlayers()) do
	task.spawn(PlayerAdded, player)
end

Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(Player: Player)
	local Profile = Profiles[Player]
	if not Profile then return end
	
	Profile:Release()
end)