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

Why is this spitting out "attempt to index nil with 'Text'"?

Asked by 3 years ago

I'm trying to make a script so you can change your keybinds, and for the most part it's been pretty smooth, however it keeps spitting out "attempt to index nil with 'Text'" in this section of code. All that section of the local script is supposed to do is change the text of the keybindButton, which is a TextButton, and that's in it's own dictionary as well to make things more organized. The keys for the keybindButtons are the exact same as the keys inside KeybindModule.defaultKeybinds. I don't really understand what's wrong here, and everything I've been doing leads to the same error. I only just learned how to manipulate dictionaries a few days ago, so maybe that's my problem lol

Module code:

-- These default keybinds are actually the values I have set --
KeybindModule.defaultKeybinds = {
    toggleInventory = "tab",  
    invDropAll = "rightClick", 
    invDropAmount = "leftClick", 

    interact = "e", 
    ping = "middleClick", 
    weapon1 = "one", 
    weapon2 = "two", 
    weapon3 = "three", 
    switchToGrenade = "g", 
    switchToOrdinance = "q", 
    melee = "f",

    crouch = "c", 
    prone = "leftControl", 
    sprint = "leftShift", 

    shoot = "leftClick", 
    aim = "rightClick", 
    reload = "r", 
    changeFireMode = "v", 
    weaponSpecial = "t"
}

function KeybindModule.getAllKeybinds(player)
    local playerSettings = PlayerData:WaitForChild(player.UserId):WaitForChild("_settings")
    local returningDictionary = {}
    for i, v in pairs(playerSettings:WaitForChild("keybinds"):GetChildren()) do
        returningDictionary[v.Name] = v.Value
    end
    return returningDictionary
end

Local script code:

local keybinds = ControlModule.getAllKeybinds(player)

for keybindKey, keybindValue in pairs(keybinds) do
    if keybindValue == nil or keybindKey == nil then
        keybindButtons[keybindKey].Text = string.upper(ControlModule.defaultKeybinds[keybindKey])
    else
        keybindButtons[keybindKey].Text = string.upper(keybindValue)
    end
end

Any help is greatly appreciated.

1
I tried doing what you did inside the Local Script and what I did is trying to print the keybindButtons[keybindKey] and it said nil acediamondn123 147 — 3y
0
what abt if you try printing it too acediamondn123 147 — 3y
0
I tried that. Thing is keybindButtons isn't nil. None of the values in keybindButtons are nil. I'll try printing the contents of keybindButtons and see what happens. acer1102 97 — 3y
0
Yup. Here's the output: {["aim"] = Keybind, ["crouch"] = Keybind, ["interact"] = Keybind, ["invDropAll"] = Keybind, ["invDropAmount"] = Keybind, ["melee"] = Keybind, ["ping"] = Keybind, ["prone"] = Keybind, ["reload"] = Keybind} It keeps going after that too, but didn't paste for some reason. acer1102 97 — 3y
View all comments (4 more)
0
This is the keybindButtons acer1102 97 — 3y
0
what if you change the keybind value instead of going trough the table and changing it. Because its already set in the talbe right? acediamondn123 147 — 3y
0
Example: keybindSettingsFrame.General.Keys.Interact.Keybind. You just change the Keybind value or whatever that is acediamondn123 147 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

I've figured it out upon going back through it. Turns out I used different variable names in a different script. It works almost flawlessly now.

Ad

Answer this question