Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to index nil on inventory script? [SOLVED]

Asked by 3 years ago
Edited 3 years ago

So I'm running this script on ServerScriptService

local module = require(script.Parent.ModuleScript)

game.Players.PlayerAdded:Connect(function(player)
    module:ChangeInventory(player, "item", 4)
end)

To reference this module in my ModuleScript to change the contents of a player's inventory, saved as a table in a datastore.

function module:ChangeInventory(player, item, value)
    print(player, item, value) --Prints Tempestcall item 4
    local UserID = "Player_" .. player.UserId
    SessionData[UserID][item] = value
    print(SessionData)
end

However when I run it I get ServerScriptService.ModuleScript:11: attempt to index nil with 'item' (Line 4 in the ModuleScript here)

Update: I realised I was using . instead of : to call ChangeInventory, the issue now is that SessionData[UserID] is nil, however this code below makes it return Stone 0

function SetupPlayerData(player)                
    local UserID = "Player_" .. player.UserId       
    local Data = Store:GetAsync(UserID)             
    if Data then                                    
        SessionData = Data
        for i, v in next, SessionData do
            print(i, v)
        end
    else 
        SessionData[UserID] = {Stone = 0}
        warn("No data found for " .. UserID)        
    end
end

Solved by putting the section following if Data then in the module code.

0
SessionData[UserId] is nil Robloxian10213104 0 — 3y

Answer this question