Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

Why does it say "Attempt to index r (a nil value)" when i literally did it the line above?

Asked by 7 days ago
Edited 7 days ago

This right here is the same script that i had a question with than yesterday, so if you think this seems familiar, thats why. So back to the problem, On Line 30 it says "Attempt to index r (a nil value)" but i literally did that a line before. Heres the script. Its (again) a script similar to Jailbreaks / Murder Mystery 2's Loot Boxes

 local m = {}

--get item
local items = require(game.ReplicatedStorage.Items)

---variables
local player = game.Players.LocalPlayer
local gui = player("WaitForChild","PlayerGui")
local ui = gui("WaitForChild","Ui")
local elements = gui("WaitForChild","Elements")

--function
    function m.getRarity(type)
        for a,b in ipairs(items.Rarities) do
            if b.name==type then
                return b
            end
        end
    end

    function m.createItem()
        local object = elements("WaitForChild","item"):Clone()
        local scaledSize = math.floor((object.Size.X.Scale*m.settings.cUI.AbsoluteSize.Y)+0.5)/m.settings.cUI.AbsoluteSize.X
        --Get / Set Types
        local types = {}
        for a,b in ipairs(m.settings.case.items) do
            for d,c in ipairs(items.Items) do
                if c.name==b then
                    local r = m:getRarity(c.rarityType)
                    for t=1,r.rarity*10 do
                        table.insert(types,c)
                    end
                end
            end
        end
        --select item
        local item = types[m.settings.prng:NextInteger(1,#types)]
        local itemType = m:getRarity(item.rarityType)

        --set other properties
        object.BorderColor3 = itemType.Color
        object.rarityMark.BackgroundColor3 = itemType.Color
        object.Name = item.name
        object.itemImage = "rbxassetid//"..item.imageID

        --set position
        object.Position = (m.settings.lastObject and
            m.settings.lastObject.Position+UDim2.new(scaledSize,0,0,0)+m.settings.offset) or UDim2.new(-scaledSize/2,0,0.5,0)
        --set parent
        object.parent = m.settings.cUI

        --set few variables
        m.settings.lastObject = object
        table.insert(m.settings.objects, object)
    end

    function m:check()
        for a,b in ipairs(m.settings.objects)do
            local scaledSize = b.AbsoluteSize
            if b.AbsolutePosition.X <= -scaledSize.x+m.settings.offset.X.Offset then
                table.remove(m.settings.objects,a)
                b:Destroy()
                m.createItem()
                m.check()
                break
            end
        end
    end

    function m:update()
        m.settings.speed = math.max(m.settings.speed-(0.03/m.settings.spinLength),0)
        m.settings.newAccel = m.settings.accel*math.sin((math.pi/2)*m.settings.speed)
        for a,b in ipairs(m.settings.objects)do
            b.Position = b.Position-UDim2.new(m.settings.newAccel/m.settings.cUI.AbsoluteSize.X,0,0,0)
        end
        if m.settings.newAccel==0 then m:result() end
    end

    function m:result()
        --disconnect
        m.render:disconnect()

        --get selected item
        local item
        for a,b in ipairs(m.settings.objects) do
            local left,right = b.AbsolutePosition.X/m.settings.cUI.AbsoluteSize.X,(b.AbsolutePosition.X+b.AbsoluteSize.X)/m.settings.cUI.AbsoluteSize.X
            if left <=0.5 and right >=0.5 then item=b break end
        end
        if item then
            m.newResult = item
        else
            local closestItems = {}
            for a,b in ipairs(m.settings.objects) do
                table.insert(closestItems,{
                mag=(Vector2.new(b.AbsolutePosition.X/m.settings.cUI.AbsoluteSize.X,0)-Vector2.new(0.5,0)).magnitude;
                item=b;
                })
            end
            table.sort(closestItems,function(a,b) return a.mag<b.mag end)
            m.newResult = closestItems[1].item
        end
    end

    function m:create(caseType)
        --Setings
        m.settings = {
            case=items.Cases[caseType];
            cUI=elements("WaitForChild","caseUI"):Clone();
            objects={};
            prng=Random.new();
            offset=UDim2.new(0,25,0,0);
            accel=30;
            speed=1;
            spinLength=5;
        }
        m.newResult=nil

        --presetup
        m.settings.cUI.Parent = ui
        for t=1,15 do
            m:createItem()
        end
        --services
        m.render = game("GetService","RunService").RenderStepped:connect(function()
            m:update()
            m:check()
        end)

        --wait for result
        repeat wait() until m.newResult

        --getrid of Ui
        spawn(function()
            wait(1)
            m.settings.cUI.TweenPosition(UDim2.new(0,0,1.15,0),"Out","Sine",1,true,function() m.settings.cUI:Destroy() end)
        end)

--return result
return m.newResult
    end

    return m

If you need more things for example my workspace, put it in the comments and ill give.

0
You mean you copied the script from them? hiimgoodpack 1851 — 7d

1 answer

Log in to vote
1
Answered by
sleazel 394 Moderation Voter
7 days ago

This should be obvious. getRarity() function sometimes simply does not return anything (no rarities?). You need to write exception, or rewrite getRarity function:

local r = m:getRarity(c.rarityType)
if r then
    for t=1,r.rarity*10 do
    table.insert(types,c)
else --r = nil
    --some stuff to do here when there are no "rarities"
end

Ad

Answer this question