I am making datastore using profile service. This is my first time doing so I want to make sure it is done correctly. Any feedback or anything that needs to be fixed will be a big help as this is my first using profile service instead of normal Roblox datastore.
local ProfileService = require(game.ServerScriptService:WaitForChild("ProfileService"))
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
-- ProfileService configurations
local ProfileStore = ProfileService.GetProfileStore(
"PlayerData",
{
money = 0,
Inventory = {
Storage = 10,
z = 0,
a = 0,
b = 0,
c = 0
}
}
)
local profiles = {}
-- Create RemoteFunction and RemoteEvent for inventory handling
local eventsFolder = replicatedStorage:FindFirstChild("Events")
if not eventsFolder then
eventsFolder = Instance.new("Folder", replicatedStorage)
eventsFolder.Name = "Events"
end
local requestInventoryData = Instance.new("RemoteFunction")
requestInventoryData.Name = "RequestInventoryData"
requestInventoryData.Parent = eventsFolder
local updateInventory = Instance.new("RemoteEvent")
updateInventory.Name = "UpdateInventory"
updateInventory.Parent = eventsFolder
local updateCurrencyEvent = Instance.new("RemoteEvent")
updateCurrencyEvent.Name = "UpdateCurrency"
updateCurrencyEvent.Parent = eventsFolder
-- Function to handle profile loading
local function loadProfile(player)
print("Loading profile for player:", player.Name)
local profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId, "ForceLoad")
if profile then
print("Profile loaded for player:", player.Name)
profile:AddUserId(player.UserId)
profile:Reconcile()
profile:ListenToRelease(function()
profiles[player] = nil
player:Kick("Your data has been released. Please rejoin.")
end)
if player:IsDescendantOf(players) then
profiles[player] = profile
local bbcc = Instance.new("Folder")
bbcc.Name = "FolderLL"
bbcc.Parent = player
local currency = Instance.new("NumberValue")
currency.Name = "Currency"
currency.Value = profile.Data.money
currency:SetAttribute("money", profile.Data.Coins)
currency.Parent = bbcc
local inventory = Instance.new("NumberValue")
inventory.Name = "inventory"
inventory.Value = 0
for attrName, attrValue in pairs(profile.Data.Backpack) do
inventory:SetAttribute(attrName, attrValue)
end
inventory.Parent = bbcc
updateCurrencyEvent:FireClient(player, currency.Value)
else
profile:Release()
end
else
player:Kick("Could not load your data. Please try again.")
end
end
-- Player added event
players.PlayerAdded:Connect(function(player)
loadProfile(player)
end)
-- Player removing event
players.PlayerRemoving:Connect(function(player)
local profile = profiles[player]
if profile then
-- Save current data before releasing the profile
local Folderll = player:FindFirstChild("FolderLL")
if Folderll then
local currency = Folderll:FindFirstChild("Currency")
if currency then
profile.Data.money = currency.Value
profile.Data.money = currency:GetAttribute("money")
end
end
profile:Release()
else
warn("Profile not found for player (PlayerRemoving):", player.Name)
end
end)
game:BindToClose(function()
for _, player in pairs(players:GetPlayers()) do
if profiles[player] then
profiles[player]:Release()
end
end
end)
-- Handle RequestInventoryData
requestInventoryData.OnServerInvoke = function(player)
local profile = profiles[player]
if profile then
print("Returning inventory data for player:", player.Name)
return profile.Data.Pets
else
warn("Profile not found for player (RequestInventoryData):", player.Name)
return {}
end
end
-- Example function to trigger UpdateInventory event (can be called when inventory changes)
local function triggerUpdateInventory(player)
updateInventory:FireClient(player)
end
While testing am I able to go on the server-side and under player go to FolderLL and change the data there like if I were to use the normal Roblox datastore and have it still save?