Instance to profile service

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")


local ReplicaService = require(ServerScriptService.ReplicaService)
local ProfileService = require(ServerScriptService.ProfileService)


local ProfileTemplate = {
	CurrentCar = "",
	Vehicles = {},
}

for _, VH in pairs(game:GetService("ReplicatedStorage").Vehicles:GetChildren()) do
	local CarData = {
		VehicleName = VH.Name,
		aOwnedV = false,
		Primary = tostring(VH.Body:FindFirstChild("PrimaryColor").Color) or "1, 1, 1",
		RimColor = "1, 1, 1",
	}
	table.insert(ProfileTemplate.Vehicles, CarData)
end


local ReplicaTestServer = {

}

local plrCars = {}

local UpdateEvent = ReplicatedStorage.Events.UpdateCustomization

local PlayerProfileClassToken = ReplicaService.NewClassToken("PlayerProfile")

local GameProfileStore = ProfileService.GetProfileStore(
	"PlayerData",
	ProfileTemplate
)

local PlayerProfile -- PlayerProfile object
local PlayerProfiles = {} -- [player] = {Profile = profile, Replica = replica}

local LastPayout = os.clock()

local function LoadData(player, profile)
	local CurrentCar = Instance.new("StringValue", player)
	CurrentCar.Name = "CurrentCar"
	CurrentCar.Value = profile.Data.CurrentCar

	local folder = Instance.new("Folder", player)
	folder.Name = "Vehicles"

	for i, v in pairs(game:GetService("ReplicatedStorage").Vehicles:GetChildren()) do
		local Cars = Instance.new("BoolValue", folder)
		Cars.Name = v.Name

		for o, a in pairs(profile.Data.Vehicles) do
			if a.VehicleName == v.Name then
				for q, e in pairs(a) do
					Cars:SetAttribute(q, e)
				end
			end
		end
	end
end
local function PlayerAdded(player)
	local profile = GameProfileStore:LoadProfileAsync(
		"#Game" .. player.UserId,
		"ForceLoad"
	)
	if profile ~= nil then
		profile:AddUserId(player.UserId)
		profile:Reconcile()
		profile:ListenToRelease(function()
			PlayerProfiles[player].Replica:Destroy()
			PlayerProfiles[player] = nil
			player:Kick()
		end)
		if player:IsDescendantOf(Players) == true then
			local player_profile = {
				Profile = profile,
				Replica = ReplicaService.NewReplica({
					ClassToken = PlayerProfileClassToken,
					Tags = {Player = player},
					Data = profile.Data,
					Replication = "All",
				}),
				_player = player,
			}
			setmetatable(player_profile, PlayerProfile)
			PlayerProfiles[player] = player_profile
			LoadData(player, profile)
		else
			profile:Release()
		end
	else
		player:Kick() 
	end
end
----- Public functions -----

-- PlayerProfile object:
PlayerProfile = {
	--[[
		_player = player,
	--]]
}
PlayerProfile.__index = PlayerProfile



function PlayerProfile:IsActive() --> is_active
	return PlayerProfiles[self._player] ~= nil
end

----- Initialize -----

for _, player in ipairs(Players:GetPlayers()) do
	coroutine.wrap(PlayerAdded)(player)
end



Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(player)
	local player_profile = PlayerProfiles[player]
	if player_profile ~= nil then
		player_profile.Profile:Release()
	end
end)

Can someone help me add new instances without the script breaking.
[the when i add new car i have to change the store for it to register the new cars]

1 Like

The simple answer is to use a serialization system.

1 Like

Mind if i ask what that is__________