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

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

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.

8 comments

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

0

what abt if you try printing it too

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.

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.

0

This is the keybindButtons

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?

0

Example: keybindSettingsFrame.General.Keys.Interact.Keybind. You just change the Keybind value or whatever that is

1 answer

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.

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