Attempt to index number with number

I want to store player’s data from a table. Here’s the code:

local DataStore = game:GetService("DataStoreService")
local sentodata = DataStore:GetDataStore("Sento")    
game.Players.PlayerAdded:Connect(function(player)	
	local data = Instance.new("Folder", player)
	data.Name = "Data"
	local toSave
	local clan = Instance.new("IntValue", data)
	local money = Instance.new("IntValue", data)
	local speed = Instance.new("IntValue", data)
	local strength = Instance.new("IntValue", data)
	local endurance = Instance.new("IntValue", data)
	local stamina = Instance.new("IntValue", data)
	local hunger = Instance.new("IntValue", data)
	
	clan.Name = "ClanName"
	money.Name = "Yen"	
	speed.Name = "Speed"
	strength.Name = "Strength"	
	endurance.Name = "Endurance"	
	stamina.Name = "Stamina"
	hunger.Name = "Hunger"
	
	local success, err = pcall(function()
		toSave = sentodata:GetAsync(player.UserId)
	end)
	
	if success then
		if type(toSave) ~= "table" then
			clan.Value = toSave[1] --Where the error occurred
			money.Value = toSave[2]
			speed.Value = toSave[3]		
			strength.Value = toSave[4]
			endurance.Value = toSave[5]
			stamina.Value = toSave[6]
			hunger.Value = toSave[7]
		else	
			clan.Value = 1	
			money.Value = 0
			speed.Value = 16			
			strength.Value = 0
			endurance.Value = 0
			stamina.Value = 0
			hunger.Value = 1						
		end
	end
	local toSave = {clan.Value, money.Value, speed.Value, strength.Value, endurance.Value, stamina.Value, hunger.Value}
	game.Players.PlayerRemoving:Connect(function(player)
		sentodata:SetAsync(player.UserId, toSave)
	end)
end)

The output said: “attempt to index number with number”

1 Like

If toSave is not a table why would you try to index it?

1 Like

I tried to change it to:

if toSave then

still that error

Well that doesnt change anything because numbers in lua are still truthy (meaning when converted to a boolean they are true). You probably just wanted type(toSave) == "table"

1 Like