Attempt to index number with 'Money'

I’m trying to store the players data using a script I dont fully understand and keep getting the below error.

18:38:46.342 ServerScriptServices.PlayerManger:85: attempt to index number with ‘Money’ - Server - PlayerManger:85

It seems the value ‘Money’ cant be found but honestly i’m not entirely sure what the error is trying to tell me.

Line 85 has been marked

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")

local function LeaderboardSetup(value)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = value
	money.Parent = leaderstats
	return leaderstats
end

local function LoadData(player)
	local success, result = pcall(function()
		return PlayerData:GetAsync(player.UserId)
	end)
	if not success  then
		warn(result)
	end
	return success, result
end

local function SaveData(player, data)
	local success, result = pcall(function()
		PlayerData:SetAsync(player.UserId, data)
	end)
	if not success then
		warn(result)
	end
	return success
end

local sessionData = {}

local playerAdded = Instance.new("BindableEvent")
local playerRemoving = Instance.new("BindableEvent")

local PlayerManager = {}

PlayerManager.PlayerAdded = playerAdded.Event
PlayerManager.PlayerRemoving = playerRemoving.Event

function PlayerManager.Start()
	for _, player in ipairs(Players:GetPlayers()) do
		coroutine.wrap(PlayerManager.OnPlayerAdded)(player)
	end
	Players.PlayerAdded:Connect(PlayerManager.OnPlayerAdded)
	Players.PlayerRemoving:Connect(PlayerManager.OnPlayerRemoving)
	
	game:BindToClose(PlayerManager.OnClose)
end

function PlayerManager.OnPlayerAdded(player)
	player.CharacterAdded:Connect(function(character)
		PlayerManager.OnCharacterAdded(player, character)
	end)
	
	local success, data = LoadData(player)
	print("shouldload")
	sessionData[player.UserId] = success and data or {
		Money = 0,
		UnlockIds = {}
	}
	
	local leaderstats = LeaderboardSetup(PlayerManager.GetMoney(player))
	leaderstats.Parent = player
	
	playerAdded:Fire(player)
end

function PlayerManager.OnCharacterAdded(player, character)
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Died:Connect(function()
			wait(2)
			player:LoadCharacter()
		end)
	end
end

function PlayerManager.GetMoney(player)
	return sessionData[player.UserId].Money -------- Line 85
end

function PlayerManager.SetMoney(player, value)
	if value then
		sessionData[player.UserId].Money = value
		
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local money = leaderstats:FindFirstChild("Money")
			if money then
				money.Value = value
			end
		end		
	end
end

function PlayerManager.OnPlayerRemoving(player)
	SaveData(player, sessionData[player.UserId])
	playerRemoving:Fire(player)
end

function PlayerManager.OnClose()
	for _, player in ipairs(Players:GetPlayers()) do
		PlayerManager.OnPlayerRemoving()
	end
end

return PlayerManager

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

The error is saying that sessionData[player.UserId] is a number, but Lua doesn’t let you use .Money on numbers.

Your code looks good, is it possible you stored a number into the datastore before you wrote this code?

The only place I see that could set sessionData[player.UserId] to a number is:

local success, data = LoadData(player)
print("shouldload")
sessionData[player.UserId] = success and data or {
	Money = 0,
	UnlockIds = {}
}

if data is already a number.

Try printing out what sessionData[player.UserId] is and what LoadData(player) returns (inside onPlayerAdded).

1 Like

@Quikiki21 why did you do success and data?

When i print out player.UserId, it prints out my money value which is currently ‘27’. I expected it to print out a table of the contents, including {Money, UnlockIds} but for some reason the value of player.UserId is actually representing ‘Money’ instead of player.UserId.Money representing ‘Money’ if you get me

1 Like

It returns data if success is true otherwise it returns the table.

I would try running some code to reset your datastore value.

Basically just run this code in the command bar

local YOUR_USER_ID = 0000000

local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")

-- Resets datastore value
PlayerData:SetAsync(YOUR_USER_ID, nil)

‘success’ is to check wether the play aleady has data and ‘data’ is to confirm the data exists. It’s straight from a tutorial so i dont know how to explain it more

1 Like

Wow that was such a simple solution, thanks for that lol

1 Like

I know this might be unrelated, but if data already exists, then it will automatically set it to data. If not, it will be the new table. So there’s no need for the success.

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