How can I fix this "attempt to index nil with touched" error?

  1. What do you want to achieve?
    I am attempting to use values in a dictionary in function(hit)

  2. What is the issue?

  3. What solutions have you tried so far?
    Moving line 113 to somewhere else in the loop
    I’ve looked through the forum, but couldn’t figure out how to fix my problem

local buttons = game.Workspace.Buttons

local JesseButtons = {
	button1 = buttons.Jesses.Jesse1,
	button2 = buttons.Jesses.Jesse2,
	button3 = buttons.Jesses.Jesse3,
	button4 = buttons.Jesses.Jesse4,
}

local function createButton(player, button)
	if player then
		if player:WaitForChild("Cash").Value >= button.Config.Price.Value then
			if db == false then
				db = true
				player.Cash.Value -= button.Config.Price.Value
				player.multiplier.Value += (button.Config.MultiValue.Value*player.skyler.Value*1.5) + button.Config.MultiValue.Value
				task.wait(0.1)
				db=false
			end
		end
	end
end

JesseButtons.Button.Touched:Connect(function(hit) -- [[breaks here]] --
local jjButtons = JesseButtons
for _, jjButtons in JesseButtons do
		local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		createButton(player, jjButtons)
		end
end)
1 Like

From what I can tell, you never define Button. (Which is why Button is nil). If Button doesn’t exist, then you can’t touch it.

Oh, I probably should’ve added an image of the workspace
image

local JesseButtons = {
	button1 = buttons.Jesses.Jesse1,
	button2 = buttons.Jesses.Jesse2,
	button3 = buttons.Jesses.Jesse3,
	button4 = buttons.Jesses.Jesse4,
}

Button isn’t a key inside of ur dictionary, has nothing to do with the workspace

1 Like

The Table is not specified, That may be why:

JesseButtons[1].Touched:Connect(function()
-- Script here
end)

Sorry I am still confused… (I am inexperienced with tables)

This is using the same dictionary and it ran without errors. I’m not sure what I am doing differently.

game.Players.PlayerAdded:Connect(function(player)
	local jButtons = JesseButtons
	for test, jButtons in JesseButtons do
		customizeButton(player, jButtons)
	end	
end)

Tables:
Usually Store certain data
Common uses of Tables is to store Players when in a Game, Change a Certain group of items, or even Names, Variables for Certain Objects, etc
Example:

local Animals = { -- "{" Marks the Beginning of the Table
"Dog", "Cat", "Rat" -- Strings Stored inside the Table
} -- "}" Marks the end of the Table

print(Animals[1])
--[[
This is print will print the First Variable inside the Table, which is "Dog"

To get the Whole Number of Items you got inside the table, do this:
]]
print(#Animals)

So in your Output, you should get something like this:

22:23:16.553  Dog  -  Server - Script:5 -- The First variable inside the table
 22:23:16.553  3  -  Server - Script:10 -- Total Length, or Amount of Items in the Table

If you write in your script:

print((Animals))

It will print this:

22:26:00.622   ▼  {
                    [1] = "Dog", -- First Variable as stated Before
                    [2] = "Cat",
                    [3] = "Rat" -- Last Variable (Total Length)
                 }  -  Server - Script:12

I hope this helps

1 Like

If I call the value directly(like below), it works.

Is there a way to do this for each value in the table, without having to call it directly like this?

button2 = buttons.Jesses.Jesse2

button2.Button.Touched:Connect(function(hit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	createButton(player, button2)
end)
for _, Items in pairs(JesseButtons) do
if string.match(Items.Name, "Button") then
Items.Button.Touched:Connect(function()
-- Code here
end
    end
end

However i recommend doing this:

for _, Items in pairs(Button.Jesses:GetChildren()) do
if string.match(Items.Name, "Button") then
Items.Button.Touched:Connect(function()
-- Code here
end
    end
end

Put this in ServerScriptService btw, wont work if you dont

It seems like it should be working, because I got no errors.
It just doesn’t work for me though, maybe something messed up somewhere else in my code

for _, Items in pairs(buttons.Jesses:GetChildren()) do
	if string.match(Items.Name, "Button") then
		Items.Button.Touched:Connect(function(hit)
			local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			createButton(player, Items)
		end)
	end
end

This is the full code, it doesn’t error and I see no logical errors

local AbbreviateGoodman = require(game.ReplicatedStorage.AbbreviateButtons)

local buttons = game.Workspace.Buttons

local db = false


--[[JESSE BUTTONS]]
local JesseButtons = {
	button1 = buttons.Jesses.Jesse1,
	button2 = buttons.Jesses.Jesse2,
	button3 = buttons.Jesses.Jesse3,
	button4 = buttons.Jesses.Jesse4,
	button5 = buttons.Jesses.Jesse5,
}


--


--[[JESSE BUTTON MANAGER]]
local function createButton(player, button)
	if player then

		if player:WaitForChild("Cash").Value >= button.Config.Price.Value then

			if db == false then
				db = true

				player.Cash.Value -= button.Config.Price.Value
				player.multiplier.Value += (button.Config.MultiValue.Value*player.skyler.Value*1.5) + button.Config.MultiValue.Value
				
				task.wait(0.1)

				db=false
			end
		end
	end
end

local function customizeButton(player, button)

	local number1 = button.Config.Price.Value
	local text1= AbbreviateGoodman:Abbreviate(number1)

	local number2 = button.Config.MultiValue.Value
	local text2 = AbbreviateGoodman:Abbreviate(number2)

	button.Button.Cost.Cash.Text = "Costs: $"..text1
	button.Button.Give.Value.Text = "Jesse: "..text2
end



game.Players.PlayerAdded:Connect(function(player)
	local jButtons = JesseButtons
	for test, jButtons in JesseButtons do
		customizeButton(player, jButtons)
	end	
end)


for _, Items in pairs(buttons.Jesses:GetChildren()) do
	if string.match(Items.Name, "Button") then
		Items.Button.Touched:Connect(function(hit)
			local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			createButton(player, Items)
		end)
	end
end

change string.match(Items.Name, "Button") to string.match(Items.Name, "Jesse"), Looking at your code, you changed Name from Button to Jesse, That’s why its not working, and the for loop is looking for items named Button

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.