Help with trying to get button

Hello

I need help with getting button from a cloned object. My code is trying to see if a player is saying a command. I’m getting error attempt to index nil with 'TextButton'

Local Script

-- Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local TeleportService = game:GetService("TeleportService")

-- Locals
local PlayerChatEvent = ReplicatedStorage:WaitForChild("PlayerChatEvent")
local MessageFolder = ReplicatedStorage:WaitForChild("MessagesFolder")
local tweenInfo = TweenInfo.new(0.3, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)
local Chat = script.Parent.Chat
local Button
local place
local Holder

local Messages = {}
--
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)

MessageFolder.ChildAdded:Connect(function(Child)
	Holder = script.Holder:Clone()
	
	local function Msgtxt()
		Holder.PlayerMessage.Text = MessageFolder.Value.Value
		Holder.PlayerName.Text = Child:WaitForChild("User").Value
	end
	
	Holder.Parent = Chat
	
	
	Button = Holder.TextButton
	
	for i, v in pairs(MessageFolder:GetChildren()) do
		if v:IsA("NumberValue") then
			place = ReplicatedStorage.MessagesFolder.place.Value
			Holder.TextButton.Visible = true
		else	
			Msgtxt()
		end
	end
	
	--TweenService:Create(Holder.PlayerMessage.Text, tweenInfo, {TextTransparency = 0, BackgroundTransparency = 0}):Play()
	
	--table.insert(Messages, {Child.Value, MessageFolder.Value.User, Holder.PlayerMessage})
	Chat.CanvasSize = UDim2.new(0, 0, 0, Chat.UIListLayout.AbsoluteContentSize.Y)
	Chat.CanvasPosition = Vector2.new(0, 9999)
end)

Holder.TextButton.MouseButton1Click:Connect(function()
	TeleportService:Teleport(ReplicatedStorage.MessagesFolder.place.Value)
end)

Which line has the error
character limit

Line 69 on Holder.TextButton.MouseButton1Click:Connect(function()
note: I did remove some code that does not impact the code show.

Do this:

-- Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local TeleportService = game:GetService("TeleportService")

-- Locals
local PlayerChatEvent = ReplicatedStorage:WaitForChild("PlayerChatEvent")
local MessageFolder = ReplicatedStorage:WaitForChild("MessagesFolder")
local tweenInfo = TweenInfo.new(0.3, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)
local Chat = script.Parent.Chat
local Button
local place
local Holder

local Messages = {}
--
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)

MessageFolder.ChildAdded:Connect(function(Child)
	Holder = script.Holder:Clone()
	
	local function Msgtxt()
		Holder.PlayerMessage.Text = MessageFolder.Value.Value
		Holder.PlayerName.Text = Child:WaitForChild("User").Value
	end
	
	Holder.Parent = Chat
	
	
	Button = Holder.TextButton
	
	for i, v in pairs(MessageFolder:GetChildren()) do
		if v:IsA("NumberValue") then
			place = ReplicatedStorage.MessagesFolder.place.Value
			Holder.TextButton.Visible = true
		else	
			Msgtxt()
		end
	end
	
	--TweenService:Create(Holder.PlayerMessage.Text, tweenInfo, {TextTransparency = 0, BackgroundTransparency = 0}):Play()
	
	--table.insert(Messages, {Child.Value, MessageFolder.Value.User, Holder.PlayerMessage})
	Chat.CanvasSize = UDim2.new(0, 0, 0, Chat.UIListLayout.AbsoluteContentSize.Y)
	Chat.CanvasPosition = Vector2.new(0, 9999)
end)

Holder:WaitForChild("TextButton").MouseButton1Click:Connect(function()
	TeleportService:Teleport(ReplicatedStorage.MessagesFolder.place.Value)
end)

Thank you but it didn’t work. I got error attempt to index nil with 'WaitForChild' .

Honestly might be a stretch here but could you try adding this in? I removed the Holder Button function from the bottom and placed it in the MessageFolder function. Once again may be a far stretch but worth a try.

MessageFolder.ChildAdded:Connect(function(Child)
	Holder = script.Holder:Clone()
	
	local function Msgtxt()
		Holder.PlayerMessage.Text = MessageFolder.Value.Value
		Holder.PlayerName.Text = Child:WaitForChild("User").Value
	end
	
	Holder.Parent = Chat
	
	
	Button = Holder.TextButton
	
	for i, v in pairs(MessageFolder:GetChildren()) do
		if v:IsA("NumberValue") then
			place = ReplicatedStorage.MessagesFolder.place.Value
			Holder.TextButton.Visible = true
		else	
			Msgtxt()
		end
	end
	
	--TweenService:Create(Holder.PlayerMessage.Text, tweenInfo, {TextTransparency = 0, BackgroundTransparency = 0}):Play()
	
	--table.insert(Messages, {Child.Value, MessageFolder.Value.User, Holder.PlayerMessage})
	Chat.CanvasSize = UDim2.new(0, 0, 0, Chat.UIListLayout.AbsoluteContentSize.Y)
	Chat.CanvasPosition = Vector2.new(0, 9999)

        Holder.TextButton.MouseButton1Click:Connect(function()
	        TeleportService:Teleport(ReplicatedStorage.MessagesFolder.place.Value)
        end)
end)

1 Like

You are getting this error because holder is nil by the time the connection is made:
You declare the holder variable by writing local Holder, which gives it a nil value. Even though you set Holder to be a clone of script.Holder in the ChildAdded event, by the time Holder.TextButton.MouseButton1Click runs, Holder is still nil because MessageFolder.ChildAdded hasn’t run yet

The solution would be to connect a function to holder inside the ChildAdded event for MessageFolder, like so:

MessageFolder.ChildAdded:Connect(function(Child)
	Holder = script.Holder:Clone()

    Holder.TextButton.MouseButton1Click:Connect(function()
	    TeleportService:Teleport(ReplicatedStorage.MessagesFolder.place.Value)
    end)


	
	local function Msgtxt()
		Holder.PlayerMessage.Text = MessageFolder.Value.Value
		Holder.PlayerName.Text = Child:WaitForChild("User").Value
	end
	
	Holder.Parent = Chat
	
	
	Button = Holder.TextButton
	
	for i, v in pairs(MessageFolder:GetChildren()) do
		if v:IsA("NumberValue") then
			place = ReplicatedStorage.MessagesFolder.place.Value
			Holder.TextButton.Visible = true
		else	
			Msgtxt()
		end
	end
	
	--TweenService:Create(Holder.PlayerMessage.Text, tweenInfo, {TextTransparency = 0, BackgroundTransparency = 0}):Play()
	
	--table.insert(Messages, {Child.Value, MessageFolder.Value.User, Holder.PlayerMessage})
	Chat.CanvasSize = UDim2.new(0, 0, 0, Chat.UIListLayout.AbsoluteContentSize.Y)
	Chat.CanvasPosition = Vector2.new(0, 9999)

Something else you should keep in mind is not to define functions in events. The Msgtxt() function should not be defined inside the MessageFolder.ChildAdded event

Thank you! I thought I couldn’t put the mouse function inside of the ChildAdded.

1 Like