Hi guys, I’m trying to make an econony system for a game I’m working on. We’re using the profile service thing to do all that… it’s working really well.
The problem we have is when a player rejoins the same server/game, whereby he is met with nil money - this is an obvious sign that the profile isn’t being fetched or whatever, I’ve got error logs to back it up.
It works perfectly the first time the player joins, but then for some reason is stuck with some duplciate token error or something?
Here’s the code, check out the error below:
--!strict
local service = {}
local players = game:GetService("Players")
local repStorage = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService")
-->> folders <<--
local shared = repStorage.Economy_Shared
local packages = shared.Packages
-->> packages <<--
local profileStore_lib = require(packages.ProfileStore)
local replica_lib = require(packages.Replica.ReplicaServer)
local table_util = require(packages.TableUtil)
-->> variables <<--
local profiles = {}
-->> dependencies <<--
local template, dataStore
do
-->> initialization <<--
function start()
initDependencies()
end
function initDependencies()
template = require(script:WaitForChild("Template"))
dataStore = profileStore_lib.New("PlayerEconomyData", template)
end
-->> events <<--
function onPlayerAdded(player: Player)
startProfile(player)
end
function onPlayerRemoving(player: Player)
endProfile(player)
end
-->> functions <<--
function startProfile(player: Player)
local profile = startProfileSession(player)
if profile then
profile:AddUserId(player.UserId) -->> GDPR compliance
profile:Reconcile() -->> fill in missing variables from PROFILE_TEMPLATE (optional)
profile.OnSessionEnd:Connect(function()
profiles[player] = nil
player:Kick(`Economy profile session end - Please rejoin`)
end)
if player.Parent == players then
profiles[player] = profile
profile.Data = table_util.Sync(profile.Data, template)
else
profile:EndSession() -->> player has left before the profile session started
end
end
if not profile then
player:Kick(`Economy profile load fail - Please rejoin`) -->> happens only when the Roblox server is shutting down
end
end
function endProfile(player: Player)
local profile = profiles[player]
assert(profile, "Profiles endProfile -> nil profile for leaving player "..player.Name)
profile:EndSession()
profiles[player] = nil
end
-->> utility <<--
function startProfileSession(player: Player)
return dataStore:StartSessionAsync(tostring(player.UserId), {
Cancel = function()
return player.Parent ~= players
end,
})
end
function add(player: Player, dataPath: {string}, amount: number)
local data = getProfileData(player)
assert(data, "Profiles add -> adding for nil player")
local dict = getProfileDataDict(data, dataPath)
local toAddData = dict[dataPath[#dataPath]]
assert(toAddData and type(toAddData) == "number", "Profiles add -> data not a number for path "..table.concat(dataPath, "."))
dict[dataPath[#dataPath]] += amount
return dict[dataPath[#dataPath]]
end
function set(player: Player, dataPath: {string}, value: any)
local data = getProfileData(player)
assert(data, "Profiles set -> setting for nil player")
local dict = getProfileDataDict(data, dataPath)
local toSetData = dict[dataPath[#dataPath]]
assert(toSetData and type(toSetData) == type(value), "Profiles set -> data not same type as value for path "..table.concat(dataPath, "."))
dict[dataPath[#dataPath]] = value
return value
end
-->> getters <<--
function getProfile(player: Player)
return profiles[player]
end
function getProfileData(player: Player, force: boolean?): typeof(template)?
local profile = getProfile(player)
if not profile then
if force then
runService.Heartbeat:Wait()
return getProfileData(player, true)
end
return nil
end
return profile.Data
end
function getProfileDataDict(profileData: typeof(template), dataPath: {string})
local dict = profileData
for i=1, #dataPath-1 do
dict = dict[dataPath[i]]
end
return dict
end
end
service.set = set
service.add = add
service.getProfileData = getProfileData
service.playerRemoving = onPlayerRemoving
service.playerAdded = onPlayerAdded
service.start = start
return service
The above image contains the error text saying “Token [my user id] duplicate”
Any ideas on how to fix it? what to do?
If the server closes or I join a new one, it works all fine, the error is only faced when a player first leaves the server and rejoins the same one.
