So I have this piece of code using ProfileService and on line 28 it returns nil for no particular reason. I’ve read the API that :LoadProfileAsync can return nil if another remote ROBLOX server attempted to load a profile at the same time but I don’t think (?) this is the case. Here’s the full code.
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptSerice = game:GetService("ServerScriptService")
local Players = game:GetService("Players")
-- Variables
local ProfileService = require(ServerScriptSerice.Modules.ProfileService)
local DataTemplate = {
Robloxians = 0;
Gold = 0;
Inventory = {};
otherData = {
IsBanned = false
}
}
local ProfileStore = ProfileService.GetProfileStore("playerData", DataTemplate)
local Profiles = {}
-- Main
local function onDataLoaded(player, profile)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name "leaderstats"
local Robloxians = Instance.new("IntValue", leaderstats)
Robloxians.Name = "Robloxians"
Robloxians.Value = profile.Data.Robloxians
local Gold = Instance.new("IntValue", leaderstats)
Gold.Name = "Gold"
Gold.Value = profile.Data.Gold
task.spawn(function()
while true do
Robloxians.Value = profile.Data.Robloxians
Gold.Value = profile.Data.Robloxians
task.wait(0.5)
end
end)
print(player.Name .. "'s data has loaded!")
end
local function onPlayerJoin(player)
local profile = ProfileService:LoadProfileAsync("Player_" .. player.UserId)
if profile ~= nil then
profile:AddUserId(player.UserId)
profile:Reconcile() -- if there are any changes to the player data template (DataTemplate), apply those changes into the profile
profile:ListenToRelease(function()
Profiles[player] = nil
player:Kick()
end)
if player:IsDescendantOf(Players) then
Profiles[player] = profile
onDataLoaded()
else
profile:Release()
end
else
player:Kick("Profile unable to load, please rejoin. :)")
end
end
local function onPlayerLeave(player)
local profile = Profiles[player]
if profile ~= nil then
profile:Release()
end
end
for _, player in pairs(Players:GetPlayers()) do
task.spawn(onPlayerJoin, player)
end
Players.PlayerAdded:Connect(onPlayerJoin)
Players.PlayerRemoving:Connect(onPlayerLeave)
```lua