Can't get a element in a table even if the element is inside the table

I was using Profile Service to manage my game’s data store but for some reason I can’t acces the player profile, at the start of the StatsInit function I define Profiles[player] as profile but if I try to return or print Profiles[player] I just receive nil, even though if I print(Profiles) I can see the “player” key, I also tried to use the user id as a key instead of player but I have the same result.

local Profiles = {}

function DataModule.statsInit(player:Player)
	local Profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)
	if Profile ~= nil then
		Profile:AddUserId(player.UserId)
		print(Profile.Data)
		Profile:Reconcile()
		Profile:ListenToRelease(function()
			Profiles[player] = nil
			player:Kick()
		end)
		if player:IsDescendantOf(game:GetService("Players")) then
			Profiles[player] = Profile
			print("Loaded")
			print(Profiles)
			print(Profile.Data)
			print(Profile.Data.PStats)
			print(Profiles[player])
			StatsModule.Init(player,Profile.Data)
			print(1)
			DataModule.Share(player)
			print(2)
		        DataModule.addToInventory(player,1)
			print(Profile.Data)
		end
	else
		player:Kick("Error while retrieving data")
	end
end

local function getProfile(player)
	print(Profiles)
	print(player)
	print(Profiles[player])
	assert(Profiles[player], "Data doesnt exist")
	return Profiles[player]
end

(The only time the getprofile() function is called is when I use the addToInventory function so the player’s profile is already inside the profiles table.

1 Like

Can you specify where the problem is happening? Is it at the assertion? Can you also provide the output?

Also I suggest renaming the Profile variable to profile. It’s extremely easy to mix up that variable with the Profiles variable.

2 Likes

The problem is happening at the 3rd last line

assert(Profiles[player], "Data doesnt exist")

When I run the script it will give an error at this line and print “data doesnt exist”, also print(Profiles[player]) only prints nil, so even though Profiles[player] exists I can’t acces it.

1 Like

Are you sure you’re passing the player into getProfile()? You can check its class name with this:

print((typeof(player) == "Instance") and player.ClassName)

If it doesn’t print “Player” then you’re not passing the player instance.

2 Likes

After adding these prints I got this output

`23:08:56.847  Player is instance ? true  -  Studio
  23:08:56.847  Player is: Player  -  Studio
  23:08:56.848   ▶ {...}  -  Studio
  23:08:56.858   ▶ {...}  -  Studio
  23:08:56.867   ▶ {...}  -  Studio
  23:08:56.868  Player is instance ? false  -  Studio
  23:08:56.868  ServerScriptService.DataModule:46: attempt to concatenate string with nil`

so it seems that the function is runnning two times, the first time it has the player as parameter but the second time player is nil.

1 Like

Okay so I found the error, I was calling the function getprofile using a function called get but to call “get” I used a colon instead of a dot so the first argument that the function getprofile was receiving wasnt player.

2 Likes

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