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

String.Sub wont remove :WaitForChild()?

Asked by
len_ny 8
6 years ago

So here's my current code:

function replaceString(s, f, r)
    s = tostring(s)
    local str, e = s:find(f)
    return s:sub(1, str-1)..r..s:sub(e+1, #s)
end

function stringToTable(s)
    local t = {}
    local currentString = ""
    for i = 1,#s do
        if string.sub(s,i,i) == "\n" then
            table.insert(t,currentString)
            currentString = ""
        else
            currentString = currentString..string.sub(s,i,i)
        end
    end
    return t
end

local linebreakTable = stringToTable(builtscript)
    local rebuild = ''
    for i,v in pairs(linebreakTable) do
        if string.find(v, 'PlayerGui') then
            v = replaceString(v, ':WaitForChild("PlayerGui")', '')
        end
        rebuild = rebuild .. string.format(tostring(v) .. '\n')
    end
    builtscript = rebuild

It should remove the :WaitForChild inside the lines that include PlayerGui right? Well ROBLOX doesn't think so.

Error: 140: attempt to perform arithmetic on local 'str' (a nil value)

I'm going to see if putting this script inside a Server-Script will fix anything.

0
The problem is in your `replaceString` function. Remember that error messages always mean what they say. You're attempting to preform arithmetic on `str` (line 4; str-1) which is a nil value. You should check if `str` exists before doing anything with it. ScriptGuider 5640 — 6y

Answer this question