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?

3 comments
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-
SetAsync will automatically encode it as a string, so you can pass number, bools, etc as long as its not userdata, functions
Try his solution, I don’t see any reason why it shouldn’t work.