Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can i fix this"attempt to index field 'selected' (a boolean value)" error?

This script is a backpack script for foraged items, but it's not working because when i click on the button i get an error that says "attempt to index field 'selected' (a boolean value)" on line 170. How do I fix this? Thanks for any help guys!

wait(3)


local SELECTED = false -- The variable
local BPBUTTON = script.Parent

local CHOICE_GUI = Instance.new("TextButton")
CHOICE_GUI.Name = "BACKPACK_CHOICE"
CHOICE_GUI.Size = UDim2.new(0, 100, 0, 25)

local CURRENT = nil

local RANGE = 15

function removeExample()
    local player = BPBUTTON.Parent.Parent.Parent
    local char = player.Character

    local ex_check = char:findFirstChild("BACKPACK_EXAMPLE")

    if ex_check ~= nil then
        ex_check:remove()
    end
end

function cleanGui()
    local player = TOOL.Parent.Parent
    local playerGui = player.PlayerGui
    local tab = playerGui.ScreenGui:GetChildren()

    for index, child in pairs(tab) do
        if child.Name == "BACKPACK_CHOICE" then
            child:remove()
        end
    end
end

function rangeCheck(mouse)
    local player = BPBUTTON.Parent.Parent.Parent
    local char = player.Character
    local torso = char.Torso

    return (torso.Position - mouse.hit.p).magnitude < RANGE
end

function do_remove_all(parent)
    local children = parent:GetChildren()

    for index, child in pairs(children) do
        child:remove()
    end
end

function onMouseMove(mouse)
    local player = BPBUTTON.Parent.Parent.Parent
    local char = player.Character
    local torso = char.Torso

    removeExample()

    if CURRENT ~= nil then
        if rangeCheck(mouse) then
            local example = CURRENT:clone()
            example.Name = "BACKPACK_EXAMPLE"
            example.Locked = true
            example.Transparency = 0.5
            example.Parent = char
            example.Position = mouse.hit.p
            do_remove_all(example)
        end
    end
end

function onButton1Down(mouse)
    if CURRENT ~= nil then
        if rangeCheck(mouse) then
            removeExample()

            CURRENT.Parent = game.Workspace
            CURRENT.Locked = false
            CURRENT.Anchored = false
            CURRENT.Position = mouse.hit.p
            CURRENT = nil
        end
    end

    update()
end

function onKeyDown(key, mouse)
    if key == "r" then
        removeExample()
        CURRENT = nil
    elseif key == "e" then
        local player = BPBUTTON.Parent.Parent.Parent
        local pack = player:findFirstChild("Pack")
        local pack_tab = pack:GetChildren()

        for index, child in pairs(pack_tab) do
            CURRENT = child
            onButton1Down(mouse)
            wait(0.1)
        end

        CURRENT = nil
    end

    update()
end

function onGuiClick(part)
    CURRENT = part
    update()
end

function update()
    local player = BPBUTTON.Parent.Parent.Parent
    local pack = player:findFirstChild("Pack")
    local pack_tab = pack:GetChildren()

    if CURRENT == nil then
        local x = 0
        local y = 250

        for index, child in pairs(pack_tab) do
            local new_gui = CHOICE_GUI:clone()
            new_gui.Position = UDim2.new(0, x, 0, y)
            new_gui.Text = child.Name
            new_gui.MouseButton1Down:connect(function() onGuiClick(child) end)
            new_gui.Parent = player.PlayerGui.ScreenGui

            y = y + 25

            if y > 350 then
                y = 250
                x = x + 100
            end
        end
    else
        cleanGui()
    end
end

function onSelected(mouse)
    if SELECTED then 
        mouse.Button1Down:connect(function() onButton1Down(mouse) end)
        mouse.Move:connect(function() onMouseMove(mouse) end)
        mouse.KeyDown:connect(function(key) onKeyDown(key, mouse) end)

        update()
    end
end

function onDeselected()
    CURRENT = nil
    removeExample()
    cleanGui()
end

function onClick() -- The function
    if SELECTED then
        SELECTED = false
        onDeselected()
    else
        SELECTED = true
        onSelected()
    end
end

BPBUTTON.Selected:connect(onSelected)
BPBUTTON.Deselected:connect(onDeselected)
BPBUTTON.MouseButton1Down:connect(onClick) 

1 answer

Well, since you said in your other script that you're trying to switch over to a gui button anyhow, I think lines 170 and 171 are useless anyway. Have you tried running it without those lines?

Alternatively, if you do still need those, you could just rename the variable "SELECTED" on line 4 and change it wherever it's referenced as well.

2 comments

0

I tried renaming the variable and now its saying Select (my new variable) isn't a member of Textbutton...

0

If you renamed it on line 170, change it back to 'Selected'.

This discussion is preserved as it was. New answers, comments, and votes are closed.