(Sorry if this is on the wrong category) So I have a module similar to PSManager, and I don’t know if I should automatically make the leaderstats inside the module. This is the code I use right now:
--Profile template for ProfileService
local PROFILE_TEMPLATE = {
Level = 1,
Gold = 0
}
--Variables that are created for leaderstats
local LEADERSTATS_TEMPLATE = {
"Coins",
"Level"
}
--Data types for createLeaderstats() function
local DATA_TYPES = {
["string"] = "StringValue",
["number"] = "IntValue",
["boolean"] = "BoolValue"
}
--Creates the leaderstats for the player. Only runs when DataManager.GLOBAL_SETTINGS.LEADERSTATS_ENABLED is true
local function createLeaderstats(player)
local playerProfile = Profiles[player]
if playerProfile ~= nil then
print("[Data Manager]: Profile found, making leaderstats")
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local startTime = os.clock()
for _, name in ipairs(LEADERSTATS_TEMPLATE) do
local data = playerProfile.Data[name]
local dataType = DATA_TYPES[type(data)]
if dataType ~= nil then
local newValue = Instance.new(dataType)
newValue.Name = name
newValue.Value = data
newValue.Parent = leaderstats
end
end
local endTime = os.clock()
return true, (endTime - startTime)
end
warn("[Data Manager]: No profile found!")
return false
end
The createLeaderstats
function runs when a player’s profile successfully loads, and I’m thinking about manually creating it on a server script and getting the data from the module. Something like this:
local DataManager = require(script.Parent.ServerModules.DataManager)
local Players = game:GetService("Players")
local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
local Level= Instance.new("IntValue")
Level.Name = "Level"
Level.Parent = leaderstats
local profile = DataManager:GetProfileOf(player)
if profile ~= nil then
Level.Value = profile.Data.Level
Coins.Value = profile.Data.Coins
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
Or make a DataManager.ProfileAdded
event and create the leaderstats once it has loaded.
DataManager.ProfileAdded:Connect(function(profile, player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = profile.Data.Coins
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Value = profile.Data.Level
end)
Thoughts about what should I do?