Attempt to index number with number?

  1. What do you want to achieve? I want to make a data system with a table. So what this does is it saves data to a table.

  2. What is the issue? The cash value wont get the amount from the table that it saves to.
    image

local LeaderstatsTable = DataStoreService:GetDataStore("Data");

local DataTable = {
	["Cash"] = 0, 
	["Miles"] = 0,
	["Level"] = 0, 
	["DailyRewardHour"] = 24,
	["DailyRewardDay"] = 0
}
local DefaultDataTable = {
	["Cash"] = 0, 
	["Miles"] = 0,
	["Level"] = 0, 
	["DailyRewardHour"] = 24,
	["DailyRewardDay"] = 0
}

local leaderstats = Instance.new("Folder");
	local gamefolder = Instance.new("Folder")
	local Cash = Instance.new("IntValue");
	local Miles = Instance.new("NumberValue");
	local Level = Instance.new("NumberValue");
	local DailyRewardHour = Instance.new("NumberValue");
	local DailyRewardDay = Instance.new("NumberValue");

	local suc, er = pcall(function()
        DataTable = LeaderstatsTable:GetAsync(player.UserId)
        print(DataTable)
    end)

    if DataTable == {} or DataTable == nil then
        DataTable = DefaultDataTable
        print("Error")
    else
        print("Success")
    end
	Cash.Value =  DataTable[1] or 0 -- Line that won't work
	Miles.Value =  DataTable[2] or 0
	Level.Value =  DataTable[3] or 0
	DailyRewardHour.Value =  DataTable[4] or 24
	DailyRewardDay.Value =  DataTable[5] or 0

Dictionaries are stored as key-value pairs and don’t have numerical indices, so DataTable[1] doesn’t exist even if the table contains something. Dictionaries aren’t ordered either. You could do DataTable["1"] if you manually enter a key-value pair with its key being [“1”] (a string)

For your application it looks like you want to do Cash.Value = DataTable.Cash or 0

Still doesn’t work. error is a bit different now. It says “attempt to index number with ‘Cash’”

What is returned by the pcall where you’re assigning
DataTable = LeaderstatsTable:GetAsync(player.UserId)?
If that call fails, what does DataTable become after DataTable = DefaultDataTable?
It may no longer be a referenceable dictionary

function PlayerLeft(Player)
	local success,err = pcall(function()
		LeaderstatsTable:SetAsync(Player.UserId,DataTable)
	end)

	if success then
		print("Success.. Thanks for playing")
	else
		warn("Error!",err)


	end
end```

Can you print(DataTable) just prior to where your Cash.Value = [...] code is? It seems DataTable is being overwritten from a dictionary to a number somewhere

Just prints 0 nothing else, not sure why

Alright, so it’s overwritten. Try adding debug statements to print DataTable whenever its changed or referenced to see where it’s being altered. Places like as you call SetAsync, when you call GetAsync, when you compared DataTable == {} [...], and where you call DataTable.Cash.

2 Likes

Only time i did setasync is when player leaves and I do getasync when player joins.