Why is my print statement outputting nil?

I’m trying to use ProfileService to setup a datastore, and I’m making a return function for when the player wants to request the player’s data.

When I do print(Profiles) it returns a table with all of the information, but when I do print(Profiles[player]) it returns nil.

Note: this is the table that is returned when I do print(Profiles)
image

Here is my code:

-- vars
local Modules = script.Parent.Parent.Parent:WaitForChild("Modules")
local Services = Modules.Services

-- services
local ProfileService = require(Services.ProfileService)

local TemplateData = {
	-- 'Template Data'
	Cash = 0
}

local Profiles = {}

local GameProfileStore = ProfileService.GetProfileStore("PlayerData1", TemplateData)

local function PlayerAdded(player)
	local profile = GameProfileStore:LoadProfileAsync(tostring(player.UserId))
	if profile then
		profile:AddUserId(player.UserId)
		profile:Reconcile()	-- 'Reconcile' handles missing template data automatically

		profile:ListenToRelease(function()
			Profiles[player] = nil
			warn("Player "..player.UserId.." was kicked for: profile loaded onto another server")
			player:Kick("Profile loaded onto another server. Please rejoin or try again later.")
		end)

		if player:IsDescendantOf(game) then
			Profiles[player] = profile
		else
			profile:Release()
		end
	else
		warn("Player "..player.UserId.." was kicked for: data did not load properly")
		player:Kick("Data did not load properly. Please rejoin or try again later.")
	end
end

local function PlayerRemoving(player)
	local profile = Profiles[player]

	if profile then
		profile:Release()
		Profiles[player] = nil
	end
end

local function GetPlayerProfile(player)
	print(Profiles)
	return Profiles[player]
end


for _, player in pairs(game.Players:GetPlayers()) do
	task.spawn(PlayerAdded(), player)
end

game.Players.PlayerAdded:Connect(function(Player)
	PlayerAdded(Player)

	task.spawn(function()
		while task.wait(1) do
			print(GetPlayerProfile())
		end
	end)
end)

game.Players.PlayerRemoving:Connect(PlayerRemoving)


game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		PlayerRemoving(player)
	end
end)

I’m trying to read your script on the phone so it’s difficult, but maybe it should be

player.UserID

on this line, you’re calling GetPlayerProfile without inputting the player


in this one, you’re calling PlayerAdded instead of passing in the function like task.spawn(PlayerAdded, player)

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