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

DataStore Error Attempt to index local 'VaribleName' (A nil value) Line 507?

Asked by 5 years ago

The Issue I'm getting is 00:34:31.785 - ServerScriptService.Clothing_DataStore:507: attempt to index local 'HotAirBalloon' (a nil value)

PasteBin For the Code

0
Gears["Hot Air Balloon"] ? , Is it good? . That was long code e.e User#17685 0 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

All of your code of just dealing with bool values? You should have seen how much code has been duplicated and changed the design.

There are far better ways of storing data than just putting value objects into the players model.

If you need to use these value objects create a function to make them. You hard code everything which is not how things should be done. Instead use loops and the correct table structures to setup, save and load data.

You error could be down to a lot of things in your code as it is not easy to add or remove itemst from a array without changing how other data is loaded.

In addition to this you have a lot of infinate loops which will cause errors and other issues if the player leaves the games.

An auto save should not be linked per player and should go through and save player data who are in the game.

There is a lot that needs to be looked at in your code and this is just one solution. There are better solutions which you should look into and this code does not account for any data store errors. I would also reccomend that you split this code up into modules.

As this code uses a differnt save / load format I have changed the key prefix to act as a new save.

Example:-

local statsSetup = {
    Shirts = {
        "RedShirt"
    },
    Hats = {
        "BlackValk Shades",
        "The Classic ROBLOX Fedora"
        -- add other hats here
    },
    Faces = {
        "Bandage",
        "Beast Mode"
        -- add other faces here
    },
    Titles = {
        "Athlete",
        "Awesome"
        -- add other titles here
    },
    Gears = {
        "Hot Air Balloon",
        "Hoverboard"
        -- add other gears here
    }
}

-- build a new instace with the data passed
local function mkInstance(insType, name, val)
    local tmp = Instance.new(insType)
    tmp.Name = name
    if val then
        tmp.Value = val
    end
    return tmp
end

local tmpStats = Instance.new("Folder")

-- loop over the table and create instances
for folderName, itemList in pairs(statsSetup) do
    local folder = mkInstance("Folder", folderName)

    for _, itm in pairs(itemList) do
        local tmpItem = mkInstance("BoolValue", itm, false)
        tmpItem.Parent = folder
    end

    folder.Parent = tmpStats
end


local ds = game:GetService("DataStoreService"):GetDataStore("Clothing")
local prefix = "usr_"
game:GetService('Players').PlayerAdded:Connect(function(plr)

    local data = ds:GetAsync(prefix .. tostring(plr.UserId))
    local stats = tmpStats:Clone()  

    -- if there is data
    if data then
        -- loop through data 
        for folderName, itemList in pairs(data) do

            -- find folder name
            local folder = stats:FindFirstChild(folderName)
            if folder then -- check if folder is found

                -- load stats for that file
                for _, itm in pairs(itemList) do
                    local plrStats = folder:FindFirstChild(itm) -- find stats
                    if plrStats then -- if stats found
                        plrStats.Value = true -- set stats to true 
                    end
                end

            end
        end
    end

    -- parent stats to the player
    for _, child in pairs(stats:GetChildren()) do
        child.Parent = plr -- parent folders to the player
    end
end)

local function saveData(plr)
    local data = {}

    -- get children of the player and save them
    -- make sure to only parent player stats to the player
    for _, folder in pairs(plr:GetChildren()) do
        if folder:IsA("Folder") then -- only save folders
            local tmp = {}
            for _, itm in pairs(folder:GetChildren()) do
                if itm.Value then -- only save true data
                    tmp[#tmp+1] = itm.Name -- save the name
                end
            end
            data[folder.Name] = tmp
        end
    end

    ds:SetAsync(prefix .. tostring(plr.UserId), data)   
end

-- call save data function
game:GetService('Players').PlayerRemoving:Connect(function(plr)
    saveData(plr)
end)

-- data does not save on server shutdown as PlayerRemoving is not called
game:BindToClose(function()
    for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
        saveData(plr)
    end
end)

This is a lot of code to look at but please comment if you do not understand how it works.

0
I understand all of it. Sorry I was away for a few days. namerplz 42 — 5y
Ad

Answer this question