local ProfileService = require(game.ReplicatedStorage.ProfileService)
local Players = game:GetService(“Players”)
local Profiles = {}
local saveStructure = {
Donated = 0;
Raised = 0;
}
local PlayerProfileStore = ProfileService.GetProfileStore(“PlayerSaveData”, saveStructure)
local function PlayerDataLoaded(player)
local profile = Profiles[player]
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local Donated = Instance.new("IntValue")
Donated.Name = "Donated"
Donated.Value = profile.Data.Donated
Donated.Parent = folder
local Raised = Instance.new("IntValue")
Raised.Name = "Raised"
Raised.Value = profile.Data.Raised
Raised.Parent = folder
spawn(function()
local profile = Profiles[player]
if profile ~= nil then
Donated.Value = profile.Data.Donated
Raised.Value = profile.Data.Raised
end
end)
end
local function PlayerAdded(player)
local profile = PlayerProfileStore:LoadProfileAsync(“Player” … player.UserId, “ForceLoad”)
if profile ~= nil then
profile:ListenToRelease(function()
Profiles[player] = nil
player:Kick(“Your profile has been loaded remotely. Please rejoin.”)
end)
if player:IsDescendantOf(Players) then
Profiles[player] = profile
PlayerDataLoaded(player)
else
profile:Release()
end
else
player:Kick("Unable to load saved data. Please rejoin.")
end
end
for , player in ipairs(Players:GetPlayers()) do
spawn(function()
PlayerAdded(player)
end)
end
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(function(player)
local profile = Profiles[player]
if profile ~= nil then
profile:Release()
end
end)
local function AddRaised(userID, amount)
local player = Players:GetPlayerByUserId(userID)
if player then
local profile = Profiles[player]
profile.Data.Raised = profile.Data.Raised + amount
end
end
local function AddDonated(userID, amount)
local player = Players:GetPlayerByUserId(userID)
if player then
local profile = Profiles[player]
profile.Data.Donated = profile.Data.Donated + amount
end
end
game.ReplicatedStorage.Events.AddValue.Event:Connect(function(userID, amount, stat)
if stat == “Raised” then
AddRaised(userID, amount)
elseif stat == “Donated” then
AddDonated(userID, amount)
end
end)
local GetDataEvent = game.ReplicatedStorage.Events.GetDataEvent
GetDataEvent.OnServerEvent:Connect(function(player, userID, stat, val)
local profile = Profiles[player]
GetDataEvent:FireClient(player, profile.Data[stat], val)
end)