I use ProfileService as my DataStore. However I am trying to access the player’s profile from a separate ModuleScript so I can access their data, but it says there is no profile.
Here is the code from the ModuleScript called Currency_ (somehow doesn’t work)
function Module.Sell(Player: Player)
local Profile = DataModule.Profiles[Player] if not Profile or Profile == nil then print("no profile") return end
end
Here’s another ServerScript that also defines Profile (somehow works)
local function Spawn(Player: Player)
local Profile = Root.Profiles[Player]
if Profile == nil or not Profile then Player:Kick("Data failed to load") print("here") return end
end
Explorer:
Inside of Root (The one I use for getting profile)
-- DO NOT MODIFY THE CODE BELOW.
local DataModule = {}
DataModule.Profiles = {}
return DataModule
local function Spawn(Player: Player)
local Profile = Root.Profiles[Player.UserId] -- You forgot userId here
if Profile == nil or not Profile then Player:Kick("Data failed to load") print("here") return end
end
function Module.Sell(Player: Player)
-- and here
local Profile = DataModule.Profiles[Player.UserId] if not Profile or Profile == nil then print("no profile") return end
end
I don’t think adding .UserId will make a difference, hence it might even create an error, it works fine with just Player but it won’t get Profile on my modulescript for some reason.
Checking if profile is nil is enough, no need for a second check. You should expose a method/function in your dataModule for retrieving a profile. Something like DataModule:GetPlayerProfile(player: Player) and then from a different module require the dataModule and call the method with the required arguments.
You also may not be waiting for the profile to even exist. If I mentioned correctly loading of profiles can take anywhere from ~7 seconds or more. So instead of immediately returning, try repeating task.wait() till the profile exists.
Yea my bad, somehow missed that lol. If I were you I would really try and expose a method to get the profile tho, directly accessing DataModule.Profiles won’t always work till the profile as loaded. Take this code for example:
local module = {
sessionProfiles = {}
}
function module:getProfile(player: Player)
local profile
repeat task.wait()
until self.sessionProfiles[player] ~= nil
profile = self.sessionProfiles[player]
return profile
end
function module:Initialize()
for _, player in ipairs(Players:GetPlayers()) do
coroutine.resume(coroutine.create(function()
self:onPlayerAdded(player)
end))
end
Players.PlayerAdded:Connect(function(player: Player)
self:onPlayerAdded(player)
end)
Players.PlayerRemoving:Connect(function(player: Player)
local profile = self.sessionProfiles[player]
if profile ~= nil then
profile:Release()
end
end)
end
I would call Initialize at the top-level portion of my main server script and then initialize all other modules and getProfile yields so it waits till the profile is available to be accessed and returns it.
I think I see the problem now.
I forgot to put Player inside one of my functions where I load player’s Profile, it works now.
Thanks for trying to help me though