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

Table datastore attempt to call a nil value?

So, this is my datastore script so far, however for some reason, it's attempting to call a nil value. Here's my script:

local DS = game:GetService("DataStoreService"):GetDataStore("Inventories")
game.Players.PlayerAdded:Connect(function(player)
	variablePlayer = player.Name
    local folder = Instance.new("Folder",player)
    folder.Name = "inventory"
    inventory = {}
    inventory.Name = "inventory"
    local dataKey = "userkey_"..player.UserId
    local data
    local success,errormsg = pcall(function()
        data = DS:GetAsync(dataKey)
    end)
    if not success then
        warn("Error when retrieving data for "..player.Name..". Error: "..errormsg)
        folder.Value = nil
    end
    end)
game.Players.PlayerRemoving:Connect(function(player)
    local success,errormsg = pcall(function()
        for i, v in pairs(game.Players[variablePlayer].inventory:GetChildren()) do
			inventory.insert(v.Value) -- This is where the error is.
			print(v.Value)
        end
	DS:SetAsync("userkey_"..player.UserId, inventory)
    end)
    if not success then
        warn("Unable to save for "..player.Name..". Error: "..errormsg)
    end
end)

Error message:

Unable to save for 7z99. Error: ServerScriptService.inventory:21: attempt to call a nil value

2 comments

0

Wouldn't you type table.insert(inventory, v.Value)?

0

You're right. Thanks! The script isn't entirely working though. I'll edit since it brought up new problems.

1 answer

✓ Accepted answer

That's because you cannot insert a value which has nothing in there your script is trying to detect for a Value since there's none it yields and error. To fix this simply put On line 21

table.Insert  (v.Value) 

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