ProfileService Kicking me because profile = nil?

So I’ve been trying to fix this issue for a while and still cannot find a clear solution to this. Upon joining my game, ProfileService kicks me because I do not have a loaded profile. I don’t know why this is happening. It works some of the times and the other times it doesn’t. Here is my OnPlayerAdded script.

local ProfileService = require(game.ServerScriptService.ProfileService)

local Players = game:GetService("Players")

local ProfileStore = ProfileService.GetProfileStore(
	"ProfileDataTEST5",
	ProfileTemplate
)

local Profiles = {}

local function PlayerAdded(player)
	local profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId,"ForceLoad")
	
	if profile ~= nil then
		profile:AddUserId(player.UserId)
		profile:Reconcile()
		profile:ListenToRelease(function()
			Profiles[player] = nil
			player:Kick("ListenToRelease Fired")
		end)
		if player:IsDescendantOf(Players) == true then
			Profiles[player] = profile
		else
			profile:Release()
		end
	else
		player:Kick("Could not find data") 
	end
end
2 Likes

The service is unreliable, especially when testing. Do not kick players if the data can’t be loaded. Handle the error correctly. The service also replies with data telling you if you are being rate limited.

1 Like

Make sure that the game settings are correct and that you can use DataStores.

It seems like you haven’t enabled those yet considering the code is fine.

1 Like

I assume it does the kick “Could not find data”?
You can try not using “ForceLoad”. The default should work fine in most cases (remove the second argument).

1 Like

What do you mean by game settings? I do have API enabled and everything.

1 Like

I will try again but I believe I did that before and it led me to using ForceLoad which also doesn’t work.

1 Like

What is the proper way to handle the data not loading?

It’s literally the default code from ProfileService which has been battle tested in many games. (despite what the docs say)

ProfileService already handles it for you. Since OP is “force-loading” the data, the profile will retry loading until it actually loads, and if the retry count exceeds a certain value (8), it will steal the data from another session (since profile service also handles session locking; the above case might happen when a user logs into the same game again before the data has been saved) and the profile will only be nil in rare cases such as when the roblox datastores decideds to be wonky or smth else, in which case there is really no other option other than to kick the player.

1 Like

It might not be an issue, I’ve honestly forgotten what I did do fix my issue because your code seems fine.