Table should be returned, but it is always nil

Currently I am working on creating a script to save the players tycoon progress. The cash save works perfectly fine, but the purchased parts have not been loading back in. It shows that they are saved when the player leaves, but when they join it is not loading them and it shows an empty table, but when they leave it has a table full of all the parts it saved even though none of them loaded in.

This is the Init() function. Don’t believe there are any issues here

function Tycoon:Init()
	self.Model = NewModel(template, self._spawn.CFrame)
	self._spawn:SetAttribute("Occupied", true)
	self.Owner.RespawnLocation = self.Model.Spawn
	self.Owner:LoadCharacter()

	self:LockAll()
	self:LoadUnlocks()
	self:WaitForExit()
end

When it calls self:LoadUnlocks() it goes to this function and this is where the issue is

function Tycoon:LoadUnlocks()
	print("Player:")
	print(self.Owner)
	print("Player Manager:")
	print(playerManager)
	print("GetUnlockIds")
	print(playerManager.GetUnlockIds())    --- The screenshot below shows what this prints
	if playerManager.GetUnlockIds() ~= nil then    --- This never continues and is always nil
		for _, id in ipairs(playerManager.GetUnlockIds(self.Owner)) do
			self:PublishTopic("Button", id)
		end
	end
end

Here you can see how it does save data, but the loading data does not working. It never gets past the
if playerManager.GetUnlockIds() ~= nil then

I also have been getting errors in this code when I remove the if statement. Where it will say attempt to index nil with “UnlockIds”. This is the function it goes to when GetUnlockIds() is called.

function PlayerManager.GetUnlockIds(player)
	if player then
		return sessionData[player.UserId].UnlockIds --- Error
	end
end

Thank you in advance to anyone who takes the time to help me figure out this issue. Feel free to let me know if you need more code, I do believe this should be most of it though.

1 Like
playerManager.GetUnlockIds()
-- is the same as
playerManager.GetUnlockIds(nil)

You’re not passing it a player. you probably want to give it self.Owner like in the following for loop

The next error about indexing nil is because sessionData[player.UserId] is nil and I would need more information to help you there.

If you’re trying to load a player that hasn’t saved maybe giving a default would help like this

function PlayerManager.GetUnlockIds(player)
	if player and sessionData[player.UserId] then -- check if player and player's data exist
		return sessionData[player.UserId].UnlockIds
    else
        return {} -- empty default instead of nil
	end
end
1 Like

Thing is I’m trying to get the unlock ids that have already been saved. In that screenhot it shows that there are values in my unlock id at the end. So why am I not able to collect these when the player rejoins?

This is the AddUnlockId code

function PlayerManager.AddUnlockId(player, id)
	local data = sessionData[player.UserId]
	
	if not table.find(data.UnlockIds, id) then
		table.insert(data.UnlockIds, id)
	end
end

You may have to initialize data.UnlockIds = {} as an empty table before using table.insert but I’m pretty sure your old code would error if it was nil.

function PlayerManager.AddUnlockId(player, id)
	local data = sessionData[player.UserId]
    if data.UnlockIds == nil then
        data.UnlockIds = {}
    end
	
	if not table.find(data.UnlockIds, id) then
		table.insert(data.UnlockIds, id)
	end
end

I don’t see the print("Saved") code so I don’t know if you’re actually saving it or using the same variable. Though I know for a fact using your GetUnlockIds without a player will give you nil

I can always send that code to show you what it looks like here in a bit.

Well I didn’t change anything and now it is working perfectly fine. I’ll get back to you if this for some reason changes, but thank you for the help!