Untitled

 avatar
unknown
plain_text
a year ago
4.5 kB
4
Indexable
-- Compiled with roblox-ts v2.2.0
local TS = require(game:GetService("ReplicatedStorage"):WaitForChild("rbxts_include"):WaitForChild("RuntimeLib"))
local Service = TS.import(script, game:GetService("ReplicatedStorage"), "rbxts_include", "node_modules", "@flamework", "core", "out").Service
local ProfileService = TS.import(script, game:GetService("ReplicatedStorage"), "rbxts_include", "node_modules", "@rbxts", "profileservice", "src")
local _services = TS.import(script, game:GetService("ReplicatedStorage"), "rbxts_include", "node_modules", "@rbxts", "services")
local Players = _services.Players
local RunService = _services.RunService
local store = TS.import(script, game:GetService("ServerScriptService"), "TS", "store").store
local selectPlayerSave = TS.import(script, game:GetService("ReplicatedStorage"), "TS", "store", "saves", "save-selector").selectPlayerSave
local defaultPlayerSave = TS.import(script, game:GetService("ReplicatedStorage"), "TS", "store", "saves", "save-types").defaultPlayerSave
local forEveryPlayer = TS.import(script, game:GetService("ReplicatedStorage"), "TS", "utils", "functions", "forEveryPlayer").forEveryPlayer
local DataStoreName = "Production"
local KEY_TEMPLATE = "%d_Data"
if RunService:IsStudio() then
	DataStoreName = "Testing"
end
local PlayerDataService
do
	PlayerDataService = setmetatable({}, {
		__tostring = function()
			return "PlayerDataService"
		end,
	})
	PlayerDataService.__index = PlayerDataService
	function PlayerDataService.new(...)
		local self = setmetatable({}, PlayerDataService)
		return self:constructor(...) or self
	end
	function PlayerDataService:constructor()
		self.profileStore = ProfileService.GetProfileStore(DataStoreName, defaultPlayerSave)
		self.profiles = {}
	end
	function PlayerDataService:onInit()
		forEveryPlayer(function(player)
			return self:createProfile(player)
		end, function(player)
			return self:removeProfile(player)
		end)
		-- task.spawn(() => {
		-- 	while (true) {
		-- 		Players.GetPlayers().forEach((player) => store.adjustPlayerCurrency(player.UserId, "Cash", 1));
		-- 		task.wait(1);
		-- 	}
		-- });
	end
	function PlayerDataService:createProfile(player)
		local userId = player.UserId
		local profileKey = string.format(KEY_TEMPLATE, userId)
		local profile = self.profileStore:LoadProfileAsync(profileKey)
		if not profile then
			return player:Kick()
		end
		profile:ListenToRelease(function()
			local _profiles = self.profiles
			local _player = player
			_profiles[_player] = nil
			store.deletePlayerSave(player.UserId)
			player:Kick()
		end)
		profile:AddUserId(userId)
		profile:Reconcile()
		local _profiles = self.profiles
		local _player = player
		_profiles[_player] = profile
		store.setPlayerSave(player.UserId, profile.Data)
		self:createLeaderstats(player)
		local unsubscribe = store:subscribe(selectPlayerSave(player.UserId), function(save)
			if save then
				profile.Data = save
			end
		end)
		Players.PlayerRemoving:Connect(function(player)
			if player == player then
				unsubscribe()
			end
		end)
	end
	function PlayerDataService:createLeaderstats(player)
		local leaderstats = Instance.new("Folder", player)
		leaderstats.Name = "leaderstats"
		local cash = Instance.new("NumberValue", leaderstats)
		cash.Name = "Cash"
		local gems = Instance.new("NumberValue", leaderstats)
		gems.Name = "Gems"
		local unsubscribe = store:subscribe(selectPlayerSave(player.UserId), function(save)
			local _result = save
			if _result ~= nil then
				_result = _result.currency.Cash
			end
			local _condition = _result
			if _condition == nil then
				_condition = 0
			end
			cash.Value = _condition
			local _result_1 = save
			if _result_1 ~= nil then
				_result_1 = _result_1.currency.Gems
			end
			local _condition_1 = _result_1
			if _condition_1 == nil then
				_condition_1 = 0
			end
			gems.Value = _condition_1
		end)
		Players.PlayerRemoving:Connect(function(player)
			if player == player then
				unsubscribe()
			end
		end)
	end
	function PlayerDataService:removeProfile(player)
		local _profiles = self.profiles
		local _player = player
		local profile = _profiles[_player]
		local _result = profile
		if _result ~= nil then
			_result:Release()
		end
	end
	function PlayerDataService:getProfile(player)
		local _profiles = self.profiles
		local _player = player
		return _profiles[_player]
	end
	PlayerDataService = Service()(PlayerDataService) or PlayerDataService
end
return {
	PlayerDataService = PlayerDataService,
}
Editor is loading...
Leave a Comment