Problem with ProfileService getting Profile

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:
image

Inside of Root (The one I use for getting profile)

-- DO NOT MODIFY THE CODE BELOW.
local DataModule = {}
DataModule.Profiles = {}

return DataModule

The slightest bit of help is greatly appreciated.

2 Likes
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
1 Like

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.

1 Like

I don’t think I need these, it was already working fine in my modulescripts. I’ll try removing the extra profile == nil though

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.

This isn’t a problem with my other scripts (which gets Profile right after the player has joined)

Could you please break down what your actual problem is? Simpler terms would be appreciated for further assistance.

1 Like

My actual problem is that I cannot get profile on the ModuleScript however getting the profile on the ServerScript works.

That is… odd. Can you show both cases of how you’re trying to access the profile. One for serverscript one for modulescripts.

What do you mean by that? The code I provided above is my method for getting profile.

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.

1 Like

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

will try this though

ignore: egsd

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.