Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Attempt to index nil with number???

I am trying to read data off a table and use it as a value in-game:

local ds = game:GetService("DataStoreService")
local invs = ds:GetDataStore("Inventory")
local plrAdded = function(plr)
	local f = Instance.new("Folder",plr)
	f.Name = "leaderstats"
	local Cash = Instance.new("IntValue",f)
	Cash.Name = "Cash"
	local Muscle = Instance.new("IntValue",f)
	Muscle.Name = "Muscle"
	local inv = Instance.new("Folder",plr)
	inv.Name = "Inventory"
	local Size = Instance.new("NumberValue",inv)
	Size.Name = "Size"
	local Page = Instance.new("Folder",inv)
	Page.Name = "Page"
	local PageOwned = Instance.new("BoolValue",Page)
	PageOwned.Name = "Owned"
	local PageEquipped = Instance.new("BoolValue",Page)
	PageEquipped.Name = "Equipped"
	for i = 50,0,-1 do
		local s,e = pcall(function()
			local store = invs:GetAsync(plr.UserId)
			Cash.Value = store[1] or 0 -- ERROR LINE
			Muscle.Value = store[2] or 0
			Size.Value = store[3] or 1
			PageOwned = store[4] or true
			PageEquipped = store[5] or true
		end)
		if e then
			warn(e.."; "..i.." retries left...")
		elseif s then
			i = 0
			break
		end
	end
	wait()
	local char = plr.Character or plr.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid")
	for i, v in pairs(hum:GetChildren()) do
		if v.Name == "HeadScale" or v.Name == "BodyDepthScale" or v.Name == "BodyWidthScale" or v.Name == "BodyHeightScale" then
			v.Value = Size.Value
			hum.WalkSpeed = Size.Value * 16
		end
	end
	Size:GetPropertyChangedSignal("Value"):Connect(function()
		for i, v in pairs(hum:GetChildren()) do
			if v.Name == "HeadScale" or v.Name == "BodyDepthScale" or v.Name == "BodyWidthScale" or v.Name == "BodyHeightScale" then
				v.Value = Size.Value
				hum.WalkSpeed = Size.Value * 16
			end
		end
	end)
end
local plrRemoving = function(plr)
	local save = {}
	table.insert(save,#save + 1,plr:WaitForChild("leaderstats"):WaitForChild("Cash").Value)
	table.insert(save,#save + 1,plr:WaitForChild("leaderstats"):WaitForChild("Muscle").Value)
	table.insert(save,#save + 1,plr:WaitForChild("Inventory"):WaitForChild("Size").Value)
	table.insert(save,#save + 1,plr:WaitForChild("Inventory"):WaitForChild("PageOwned").Value)
	table.insert(save,#save + 1,plr:WaitForChild("Inventory"):WaitForChild("PageEquipped").Value)
end
game.Players.PlayerAdded:Connect(function(plr)
	coroutine.wrap(plrAdded(plr))
end)
game.Players.PlayerRemoving:Connect(function(plr)
	coroutine.wrap(plrRemoving(plr))
end)

It warns me this:

Workspace.ScriptName:23:Attempt to index nil with number

How do I fix this?

2 answers

:GetAsync returns the value stored at that datastore. Datastores can only hold strings, so watch out and remember to use HTTPService's :JSONEncode and :JSONDecode functions when saving tables.

However, what appears to be happening is that you're not checking if the data there even exists. The data is nil because there is nothing stored in that datastore. A simple if statement should do the trick.

if Data ~= nil then, for example.

3 comments

0

no, i used data stores for tables a few weeks ago and it worked but i forgot exactly what i did and i didnt save the script as it was a test-

0

SetAsync will automatically encode it as a string, so you can pass number, bools, etc as long as its not userdata, functions

0

Try his solution, I don’t see any reason why it shouldn’t work.

It errored on line 23 because you did nil.1, store is nil, because GetAsync returns nil if there is no value stored for that, you should add a or there and give it a default table

This discussion is preserved as it was. New answers, comments, and votes are closed.