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

Attempted to concatenate a nil value? [SOLVED]

Asked by 10 years ago

I'm making a ROBLOX implementation of CSS (to be used with GUIs) with a ModuleScript, and I'm getting an error. (These scripts are long but you shouldn't need to read the whole thing)

ModuleScript:

local module = {}

local function splitStylesheet(sheet)
    local split = {}

    local char = 1
    local line = 1
    for i = 1, string.len(sheet) do
        if string.sub(sheet, char, char) ~= "}" then
            table.insert(split, line, split[line]..string.sub(sheet, char, char))
            char = char + 1
        else
            line = line + 1
            char = char + 1
        end
    end

    return split
end

local function findSubstyles(dump)
    print("Splitting dump...")

    local open = 0
    local close = 0

    for brackets in string.gmatch(dump, "{") do
        open = open + 1
    end

    for brackets in string.gmatch(dump, "}") do
        close = close + 1
    end

    if open == close then
        local substyles = open

        return substyles
    elseif open > close then
        return "open"
    elseif close > open then
        return "close"
    else
        return "fatal"
    end
end

module.readStylesheet = function(name, dump)
    print("Reading stylesheet \""..name.."\"")

    if not name or not dump then
        error("Missing arg (function readStylesheet)")
        script:Destroy()
    end

    -- find number of substyles
    local substyles = findSubstyles(dump)

    if substyles == "open" then
        error("Assembler detected stray open bracket. Please check stylesheet")
        script:Destroy()
    elseif substyles == "close" then
        error("Assembler detected stray close bracket. Please check stylesheet")
        script:Destroy()
    elseif substyles == "fatal" then
        error("Assembler encountered fatal error. Please check stylesheet")
        script:Destroy()
    else
        print(substyles.." substyles found")
    end

    -- split substyles into a table
    local styletable = splitStylesheet(dump)

    print(styletable)
end

return module

Script I'm using the Module with (it's in a ScreenGui):

local css = require(game.Workspace.CSS)

css.readStylesheet("stylesheet1", "constructor1{background:none;}constructor2{background:none;}")

Output:

20:53:53.339 - Workspace.CSS:10: attempt to concatenate field '?' (a nil value)
20:53:53.359 - Stack Begin
20:53:53.359 - Script 'Workspace.CSS', Line 10 - upvalue splitStylesheet
20:53:53.360 - Script 'Workspace.CSS', Line 73 - field readStylesheet
20:53:53.360 - Script 'Players.Player.PlayerGui.PlayerGui.Main', Line 3
20:53:53.361 - Stack End

Anyone know what the problem is? Thanks to anyone who helps :)

0
string[line] is nil while you're still inserting the value into the table, so you're going to have to find a way around using string[line]. I would also suggest using tostring() to make sure the entire value you're going to insert is a string. Spongocardo 1991 — 10y
0
Not sure what you mean by this... DevJackB 55 — 10y
1
On line 10 when you attempt to call split[line], there is no value in spilt at the spot corresponding with the value for line. You then attempt to concatenate that value (nil) with something else, which is why the error is being thrown. BlackJPI 2658 — 10y
0
Couldn't have put it any better myself @Masta Spongocardo 1991 — 10y

Answer this question