Attempt to index nil with 'MouseEnter'

Hello, I am having an issue with this script. I’m unsure of how I would fix this table as it returns the error “attempt to index nil with MouseEnter.” It is supposed to change a few UI text and colours when the player hovers over the said UI. Any Help is appreciated. I’m relatively new to using tables so I’m not really sure on how some of this stuff works correctly.

I know that i/v is being returned as nil, but i don’t know how i could resolve this correctly.

plr = game.Players.LocalPlayer
char = plr.Character or plr.CharacterAdded:Wait()
class = plr.PlayerScripts:FindFirstChild("Class")
KitRemoteEvent = game.ReplicatedStorage.KitRE
MainGUI = script.Parent


--Flavour Text for exiting button descriptions.
FlavourText = {
	"How's your day been?",
	"Exclusive Classes are exclusive to specific players.",
	"...",
	"All classes should be oriented around playstyle, so feel free to take them all out for a test!",
}

-- Classes
Classes = {
	None = {BGColor = ColorSequence.new{ColorSequenceKeypoint.new(0, Color3.fromRGB(39,39,39)), ColorSequenceKeypoint.new(1, Color3.new(0,0,0))}},
	Offense = {Button = MainGUI.Offense, BGColor = ColorSequence.new{ColorSequenceKeypoint.new(0, MainGUI.Offense.MainColor.Value), ColorSequenceKeypoint.new(1, Color3.new(0,0,0))}, Text = MainGUI.Offense.Description.Value},
	Defense = {Button = MainGUI.Defense, BGColor = ColorSequence.new{ColorSequenceKeypoint.new(0, MainGUI.Defense.MainColor.Value), ColorSequenceKeypoint.new(1, Color3.new(0,0,0))}, Text = MainGUI.Defense.Description.Value},
	Support = {Button = MainGUI.Support, BGColor = ColorSequence.new{ColorSequenceKeypoint.new(0, MainGUI.Support.MainColor.Value), ColorSequenceKeypoint.new(1, Color3.new(0,0,0))}, Text = MainGUI.Support.Description.Value},
	Dreamer = {Button = MainGUI.Dreamer, BGColor = ColorSequence.new{ColorSequenceKeypoint.new(0, MainGUI.Dreamer.MainColor.Value), ColorSequenceKeypoint.new(1, Color3.new(0,0,0))}, Text = MainGUI.Dreamer.Description.Value},
	Riyeten = {Button = MainGUI.Riyeten, BGColor = ColorSequence.new{ColorSequenceKeypoint.new(0, MainGUI.Riyeten.MainColor.Value), ColorSequenceKeypoint.new(1, Color3.new(0,0,0))}, Text = MainGUI.Riyeten.Description.Value},
	WIP = {Button = MainGUI.WIP, BGColor = ColorSequence.new{ColorSequenceKeypoint.new(0, MainGUI.WIP.MainColor.Value), ColorSequenceKeypoint.new(1, Color3.new(0,0,0))}, Text = MainGUI.WIP.Description.Value}
}

confirmation = false

function SetClassInfo(Col, Txt)
	if confirmation == false then
		MainGUI.Background.UIGradient.Color = Col
		MainGUI.TopText.ClassDesc.Text = Txt
	end
end

for i, v in pairs(Classes) do

	v.Button.MouseEnter:Connect(function()
		print(i)
		print(v)
		local class = Classes[i]
		print(class)
		SetClassInfo(class.BGColor,class.Text)
	end)
	
	v.Button.MouseLeave:Connect(function()
		local FT = FlavourText[math.random(1,#FlavourText)]
		local class = Classes.None
		SetClassInfo(class.BGColor,FT)
	end)
end

Your None class doesn’t have Button value

None = {BGColor = ColorSequence.new{ColorSequenceKeypoint.new(0, Color3.fromRGB(39,39,39)), ColorSequenceKeypoint.new(1, Color3.new(0,0,0))}},

That’s why it says that v.Button is nil

Add some sort of check for that

if v["Button"] then
    v.Button.MouseEnter:Connect(function()
		print(i)
		print(v)
		local class = Classes[i]
		print(class)
		SetClassInfo(class.BGColor,class.Text)
	end)
end

Thanks!
The None Class holds information that the script will use once the player’s mouse leaves any of the buttons. As such, the class itself doesn’t have its own button that I can assign it to, and I don’t think making a fake one would be a professional way of resolving that

Is there a way to have the script ignore that class since it’s only used when referred to directly (meaning it doesn’t need to be detected in the for loop?

You can check if the table has the key “Button” by trying to get it

None = {}
print(None["Button"]) -- output: nil
if None["Button"] then -- checks if it is not nil
  -- do something
end

So your code should look like this:

plr = game.Players.LocalPlayer
char = plr.Character or plr.CharacterAdded:Wait()
class = plr.PlayerScripts:FindFirstChild("Class")
KitRemoteEvent = game.ReplicatedStorage.KitRE
MainGUI = script.Parent


--Flavour Text for exiting button descriptions.
FlavourText = {
	"How's your day been?",
	"Exclusive Classes are exclusive to specific players.",
	"...",
	"All classes should be oriented around playstyle, so feel free to take them all out for a test!",
}

-- Classes
Classes = {
	None = {BGColor = ColorSequence.new{ColorSequenceKeypoint.new(0, Color3.fromRGB(39,39,39)), ColorSequenceKeypoint.new(1, Color3.new(0,0,0))}},
	Offense = {Button = MainGUI.Offense, BGColor = ColorSequence.new{ColorSequenceKeypoint.new(0, MainGUI.Offense.MainColor.Value), ColorSequenceKeypoint.new(1, Color3.new(0,0,0))}, Text = MainGUI.Offense.Description.Value},
	Defense = {Button = MainGUI.Defense, BGColor = ColorSequence.new{ColorSequenceKeypoint.new(0, MainGUI.Defense.MainColor.Value), ColorSequenceKeypoint.new(1, Color3.new(0,0,0))}, Text = MainGUI.Defense.Description.Value},
	Support = {Button = MainGUI.Support, BGColor = ColorSequence.new{ColorSequenceKeypoint.new(0, MainGUI.Support.MainColor.Value), ColorSequenceKeypoint.new(1, Color3.new(0,0,0))}, Text = MainGUI.Support.Description.Value},
	Dreamer = {Button = MainGUI.Dreamer, BGColor = ColorSequence.new{ColorSequenceKeypoint.new(0, MainGUI.Dreamer.MainColor.Value), ColorSequenceKeypoint.new(1, Color3.new(0,0,0))}, Text = MainGUI.Dreamer.Description.Value},
	Riyeten = {Button = MainGUI.Riyeten, BGColor = ColorSequence.new{ColorSequenceKeypoint.new(0, MainGUI.Riyeten.MainColor.Value), ColorSequenceKeypoint.new(1, Color3.new(0,0,0))}, Text = MainGUI.Riyeten.Description.Value},
	WIP = {Button = MainGUI.WIP, BGColor = ColorSequence.new{ColorSequenceKeypoint.new(0, MainGUI.WIP.MainColor.Value), ColorSequenceKeypoint.new(1, Color3.new(0,0,0))}, Text = MainGUI.WIP.Description.Value}
}

confirmation = false

function SetClassInfo(Col, Txt)
	if confirmation == false then
		MainGUI.Background.UIGradient.Color = Col
		MainGUI.TopText.ClassDesc.Text = Txt
	end
end

for i, v in pairs(Classes) do
    if v["Button"] then
		v.Button.MouseEnter:Connect(function()
			print(i)
			print(v)
			local class = Classes[i]
			print(class)
			SetClassInfo(class.BGColor,class.Text)
		end)
	
		v.Button.MouseLeave:Connect(function()
			local FT = FlavourText[math.random(1,#FlavourText)]
			local class = Classes.None
			SetClassInfo(class.BGColor,FT)
		end)
    end
end

Thanks! I still got the error when the game was starting up since the if statement was placed inside of the function so I just moved it to check right after the for loop. even then it still worked regardless so again, thanks for your help :smiley:

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