Button being clicked but function is not running

So I have a little chunk in my script here: (Sorry for the weird indentation)

                            local button = returned[2]:WaitForChild(returned[3])
							print(button.Name)
							print(button.ClassName)
							button.Activated:Connect(function()
								print("activated")
								if tabl[value][3] == false then
									if tabl[value][5] then
										if tabl[value][5]() == true then
											if clickDebounce == false then
												clickDebounce = true
												print "button clicked"
												print "plr"
												wait(.15)
												script.Parent.Parent.Current.Value += 1
												tabl[value][3] = true
												wait(0.25)
												clickDebounce = false
											end
										end
									else--do the same thing
										if clickDebounce == false then
											clickDebounce = true
											print "button clicked"
											print "plr"
											wait(.15)
											script.Parent.Parent.Current.Value += 1
											tabl[value][3] = true
											wait(0.25)
											clickDebounce = false
										end
									end
								end
							end)

And you can see that there is a function for when a button is activated.

You shouldn’t need the rest of my code, but if you do, just ask me!

My output shows that button.Name is “SmallMachine1” and button.ClassName is “TextButton”. It doesn’t at all print “activated”. This is telling me that the function is not running.

How will I fix this? Thanks for any help, I appreciate it :slight_smile:

This is the whole script:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local character = plr.Character or plr.CharacterAdded:Wait()

local tabl = {
	[1] = {
		"Well, well, well. Look who we have here. A newbie!",
		function()
			return {"click"}
		end,
		false,
		"Click to continue."
	},
	[2] = {
		"You probably are wondering why you are here. You need to complete the tutorial!",
		function()
			return {"click"}
		end,
		false,
		"Click to continue."
	},
	[3] = {
		"First off, you need to claim a plot. Touch the yellow button to claim a plot.",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Essentials.Claim}
		end,
		false,
		"Claim a plot to continue."
	},
	[4] = {
		"Great job! Now, you must buy a conveyor. This is essential to your tycoon.",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Buttons.Create["Buy Conveyor - $1K"].Button}
		end,
		false,
		"Buy a conveyor to continue."
	},
	[5] = {
		"Awesome! Buy a chocoblox seller so when chocoblox is dropped it can be sold for cash!",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Buttons.Create["Buy Chocoblox Seller - $500"].Button}
		end,
		false,
		"Buy a chocoblox seller to continue."
	},
	[6] = {
		"Now you can buy a chocoblox machine that drops chocoblox. The chocoblox will move along the conveyor, eventually being sold.",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Buttons.Create["Buy Chocoblox Machine - $500"].Button}
		end,
		false,
		"Buy a chocoblox machine to continue."
	},
	[7] = {
		"Chocoblox is dropping, but how do you collect cash? You can collect cash by touching a green part. But where is it?",
		function()
			return {"click"}
		end,
		false,
		"Click to continue."
	},
	[8] = {
		"Try finding it, and when you find it, touch it and collect it!",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Essentials.GetCash.GetCash}
		end,
		false,
		"Collect some cash to continue."
	},
	[9] = {
		"Once you have enough cash, you can buy more machines!",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Buttons.Create["Buy Chocoblox Machine - $750"].Button}
		end,
		false,
		"Buy two more machines to continue.",
		function() --exception
			if plr.leaderstats.Cash.Value >= 750 then
				return true
			else
				return false
			end
		end
	},
	[10] = {
		"Now, buy some walls so I can show you something amazing...",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Buttons.Create["Buy Walls - $100"].Button}
		end,
		false,
		"Buy walls to continue.",
		function() --exception
			if plr.leaderstats.Cash.Value >= 100 then
				return true
			else
				return false
			end
		end
	},
	[11] = {
		"Woah, what is that red button?! Touch it, and something marvelous will happen...",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Buttons.Destroyed["Make Doorways"].Button}
		end,
		false,
		"Touch the red button to continue."
	},
	[12] = {
		"You see what happened? Blue buttons create, while red buttons destroy.",
		function()
			return {"click"}
		end,
		false,
		"Click to continue."
	},
	[13] = {
		"A gray button has appeared! These buttons allow you to choose what you want to buy. You can sell the item you buy and get a more expensive item. For now just buy the SmallMachine1.",
		function()
			return {"uiclick",plr.PlayerGui.ChooseButton.Frame.Purchase,"SmallMachine1"}
		end,
		false,
		"Buy SmallMachine1 to continue.",
		function() --exception
			if plr.leaderstats.Cash.Value >= 1000 then
				return true
			else
				return false
			end
		end
	},
	[14] = {
		"When you have enough money, you can sell the machine and buy it again!",
		function()
			return {"uiclick",plr.PlayerGui.DeleteObject.Frame.Yes}
		end,
		false,
		"Get $2250 then sell the machine to continue.",
		function() --exception
			if plr.leaderstats.Cash.Value >= 2250 then
				return true
			else
				return false
			end
		end
	},
	[15] = {
		"Buy MediumMachine1 now!",
		function()
			return {"touch",plr.PlayerGui.ChooseButton.Frame.Purchase,"MediumMachine1"}
		end,
		false,
		"Buy MediumMachine1 to continue.",
		function() --exception
			if plr.leaderstats.Cash.Value >= 2250 then
				return true
			else
				return false
			end
		end
	},
	[16] = {
		"Awesome work! You can now teleport to the game.",
		function()
			return {"click"}
		end,
		false,
		"blah continue."
	},
}

local clickDebounce = false

game.ReplicatedStorage.Remotes.Functions.CheckTutorial.OnClientInvoke = function(value)
	if script.Parent.Parent.Current.Value == value then
		return true
	else
		return false
	end
end

script.Parent.Parent.Current.Changed:Connect(function(value)
	if tabl[value] then
		if tabl[value + 1] ~= nil then
			if not tabl[value][3] then
				script.Parent.Text = tabl[value][1]
				script.Parent.Parent.Info.Text = tabl[value][4]

				local returned = tabl[value][2]()
				if returned[1] == "click" then
					print "click"
					mouse.Button1Up:Connect(function()
						if tabl[value][3] == false then
							if tabl[value][5] then
								if tabl[value][5]() == true then
									if clickDebounce == false then
										clickDebounce = true
										wait(.15)
										script.Parent.Parent.Current.Value += 1
										tabl[value][3] = true
										wait(0.25)
										clickDebounce = false
									end
								end
							else--do the same thing
								if clickDebounce == false then
									clickDebounce = true
									wait(.15)
									script.Parent.Parent.Current.Value += 1
									tabl[value][3] = true
									wait(0.25)
									clickDebounce = false
								end
							end
						end
					end)
				elseif returned[1] == "touch" then
					print "touch"
					if returned[2] and returned[2]:IsA("BasePart") then
						print "basepart"
						returned[2].Touched:Connect(function(hit)
							if tabl[value][3] == false then
								if tabl[value][5] then
									if tabl[value][5]() == true then
										if clickDebounce == false then
											clickDebounce = true
											print "touched"
											if hit.Parent:FindFirstChild("Humanoid") then
												print "plr"
												wait(.15)
												script.Parent.Parent.Current.Value += 1
												tabl[value][3] = true
											end
											wait(0.25)
											clickDebounce = false
										end
									end
								else--do the same thing
									if clickDebounce == false then
										clickDebounce = true
										print "touched"
										if hit.Parent:FindFirstChild("Humanoid") then
											print "plr"
											wait(.15)
											script.Parent.Parent.Current.Value += 1
											tabl[value][3] = true
										end
										wait(0.25)
										clickDebounce = false
									end
								end
							end
						end)
					end
				elseif returned[1] == "uiclick" then
					print "uiclick"
					if returned[2] then
						print "returned"
						if returned[2]:IsA("GuiButton") then
							print "2 is guibutton"
							returned[2].MouseButton1Click:Connect(function()
								print "activated"
								if tabl[value][3] == false then
									if tabl[value][5] then
										if tabl[value][5]() == true then
											if clickDebounce == false then
												clickDebounce = true
												print "button clicked"
												print "plr"
												wait(.15)
												script.Parent.Parent.Current.Value += 1
												tabl[value][3] = true
												wait(0.25)
												clickDebounce = false
											end
										end
									else--do the same thing
										if clickDebounce == false then
											clickDebounce = true
											print "button clicked"
											print "plr"
											wait(.15)
											script.Parent.Parent.Current.Value += 1
											tabl[value][3] = true
											wait(0.25)
											clickDebounce = false
										end
									end
								end
							end)
						elseif returned[3] then
						--	print(returned[3])
							local UIbutton = returned[2]:WaitForChild(returned[3])
							print(UIbutton.Name)
							print(UIbutton.ClassName)
							UIbutton.MouseButton1Click:Connect(function()
								print("activated")
								if tabl[value][3] == false then
									if tabl[value][5] then
										if tabl[value][5]() == true then
											if clickDebounce == false then
												clickDebounce = true
												print "button clicked"
												print "plr"
												wait(.15)
												script.Parent.Parent.Current.Value += 1
												tabl[value][3] = true
												wait(0.25)
												clickDebounce = false
											end
										end
									else--do the same thing
										if clickDebounce == false then
											clickDebounce = true
											print "button clicked"
											print "plr"
											wait(.15)
											script.Parent.Parent.Current.Value += 1
											tabl[value][3] = true
											wait(0.25)
											clickDebounce = false
										end
									end
								end
							end)
						end
					end
				else
					print "none"
				end
			end
		elseif tabl[value + 1] == nil then
			script.Parent.Parent.Info.Visible = false
			script.Parent.Text = tabl[value][1]
			
			script.Parent.Parent.Teleport.Visible = true 
		end
	end
end)

wait(3)
script.Parent.Visible = true
script.Parent.Parent.Info.Visible = true
script.Parent.Parent.Current.Value = 1

Did you try using MouseButton1Click() ? If it’s a TextButton use MouseButton1Click

Doesn’t work. I’m pretty sure Activated doesn’t work, MouseButton1Click, MouseButton1Up, etc.

I think you should change to Button1Down, it will detect when player clicked their mouse once, Mouse1Up is a function that detect when player hold and release their mouse. Try using Mouse1Down instead, I think it will work.

 mouse.Button1Down:Connect(function()
       print("Player clicked!")
 end)

As far as I know, this is me clicking a button. I already did this in this part of the code:

				if returned[1] == "click" then
					print "click"
					mouse.Button1Up:Connect(function()
						if tabl[value][3] == false then

I will try doing MouseButton1Down or whatever I need to do.

MouseButton1Click doesnt work with the TextButton?

Have you try a script like this?

button.MouseButton1Click:Connect(function()
    print("Actived!")
end)

Edit : Make sure the button is not a nil value until it got clicked.

local UIbutton = returned[2]:WaitForChild(returned[3])
if UIbutton ~= nil then
	print("it is not nil!")
end
UIbutton.MouseButton1Click:Connect(function()
	print("clicked function running")
end)

It is not nil, but I don’t see any printed statement in the output stating “clicked function running”.

At this point I am going to try random things to see if they work.

Full new script:

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local character = plr.Character or plr.CharacterAdded:Wait()

local tabl = {
	[1] = {
		"Well, well, well. Look who we have here. A newbie!",
		function()
			return {"click"}
		end,
		false,
		"Click to continue."
	},
	[2] = {
		"You probably are wondering why you are here. You need to complete the tutorial!",
		function()
			return {"click"}
		end,
		false,
		"Click to continue."
	},
	[3] = {
		"First off, you need to claim a plot. Touch the yellow button to claim a plot.",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Essentials.Claim}
		end,
		false,
		"Claim a plot to continue."
	},
	[4] = {
		"Great job! Now, you must buy a conveyor. This is essential to your tycoon.",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Buttons.Create["Buy Conveyor - $1K"].Button}
		end,
		false,
		"Buy a conveyor to continue."
	},
	[5] = {
		"Awesome! Buy a chocoblox seller so when chocoblox is dropped it can be sold for cash!",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Buttons.Create["Buy Chocoblox Seller - $500"].Button}
		end,
		false,
		"Buy a chocoblox seller to continue."
	},
	[6] = {
		"Now you can buy a chocoblox machine that drops chocoblox. The chocoblox will move along the conveyor, eventually being sold.",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Buttons.Create["Buy Chocoblox Machine - $500"].Button}
		end,
		false,
		"Buy a chocoblox machine to continue."
	},
	[7] = {
		"Chocoblox is dropping, but how do you collect cash? You can collect cash by touching a green part. But where is it?",
		function()
			return {"click"}
		end,
		false,
		"Click to continue."
	},
	[8] = {
		"Try finding it, and when you find it, touch it and collect it!",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Essentials.GetCash.GetCash}
		end,
		false,
		"Collect some cash to continue."
	},
	[9] = {
		"Once you have enough cash, you can buy more machines!",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Buttons.Create["Buy Chocoblox Machine - $750"].Button}
		end,
		false,
		"Buy two more machines to continue.",
		function() --exception
			if plr.leaderstats.Cash.Value >= 750 then
				return true
			else
				return false
			end
		end
	},
	[10] = {
		"Now, buy some walls so I can show you something amazing...",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Buttons.Create["Buy Walls - $100"].Button}
		end,
		false,
		"Buy walls to continue.",
		function() --exception
			if plr.leaderstats.Cash.Value >= 100 then
				return true
			else
				return false
			end
		end
	},
	[11] = {
		"Woah, what is that red button?! Touch it, and something marvelous will happen...",
		function()
			return {"touch",workspace.Tycoon.Plots.Plot1.Buttons.Destroyed["Make Doorways"].Button}
		end,
		false,
		"Touch the red button to continue."
	},
	[12] = {
		"You see what happened? Blue buttons create, while red buttons destroy.",
		function()
			return {"click"}
		end,
		false,
		"Click to continue."
	},
	[13] = {
		"A gray button has appeared! These buttons allow you to choose what you want to buy. You can sell the item you buy and get a more expensive item. For now just buy the SmallMachine1.",
		function()
			return {"uiclick",plr.PlayerGui.ChooseButton.Frame.Purchase,"SmallMachine1"}
		end,
		false,
		"Buy SmallMachine1 to continue.",
		function() --exception
			if plr.leaderstats.Cash.Value >= 1000 then
				return true
			else
				return false
			end
		end
	},
	[14] = {
		"When you have enough money, you can sell the machine and buy it again!",
		function()
			return {"uiclick",plr.PlayerGui.DeleteObject.Frame.Yes}
		end,
		false,
		"Get $2250 then sell the machine to continue.",
		function() --exception
			if plr.leaderstats.Cash.Value >= 2250 then
				return true
			else
				return false
			end
		end
	},
	[15] = {
		"Buy MediumMachine1 now!",
		function()
			return {"touch",plr.PlayerGui.ChooseButton.Frame.Purchase,"MediumMachine1"}
		end,
		false,
		"Buy MediumMachine1 to continue.",
		function() --exception
			if plr.leaderstats.Cash.Value >= 2250 then
				return true
			else
				return false
			end
		end
	},
	[16] = {
		"Awesome work! You can now teleport to the game.",
		function()
			return {"click"}
		end,
		false,
		"blah continue."
	},
}

local clickDebounce = false

game.ReplicatedStorage.Remotes.Functions.CheckTutorial.OnClientInvoke = function(value)
	if script.Parent.Parent.Current.Value == value then
		return true
	else
		return false
	end
end

script.Parent.Parent.Current.Changed:Connect(function(value)
	if tabl[value] then
		if tabl[value + 1] ~= nil then
			if not tabl[value][3] then
				script.Parent.Text = tabl[value][1]
				script.Parent.Parent.Info.Text = tabl[value][4]

				local returned = tabl[value][2]()
				if returned[1] == "click" then
					print "click"
					mouse.Button1Up:Connect(function()
						if tabl[value][3] == false then
							if tabl[value][5] then
								if tabl[value][5]() == true then
									if clickDebounce == false then
										clickDebounce = true
										wait(.15)
										script.Parent.Parent.Current.Value += 1
										tabl[value][3] = true
										wait(0.25)
										clickDebounce = false
									end
								end
							else--do the same thing
								if clickDebounce == false then
									clickDebounce = true
									wait(.15)
									script.Parent.Parent.Current.Value += 1
									tabl[value][3] = true
									wait(0.25)
									clickDebounce = false
								end
							end
						end
					end)
				elseif returned[1] == "touch" then
					print "touch"
					if returned[2] and returned[2]:IsA("BasePart") then
						print "basepart"
						returned[2].Touched:Connect(function(hit)
							if tabl[value][3] == false then
								if tabl[value][5] then
									if tabl[value][5]() == true then
										if clickDebounce == false then
											clickDebounce = true
											print "touched"
											if hit.Parent:FindFirstChild("Humanoid") then
												print "plr"
												wait(.15)
												script.Parent.Parent.Current.Value += 1
												tabl[value][3] = true
											end
											wait(0.25)
											clickDebounce = false
										end
									end
								else--do the same thing
									if clickDebounce == false then
										clickDebounce = true
										print "touched"
										if hit.Parent:FindFirstChild("Humanoid") then
											print "plr"
											wait(.15)
											script.Parent.Parent.Current.Value += 1
											tabl[value][3] = true
										end
										wait(0.25)
										clickDebounce = false
									end
								end
							end
						end)
					end
				elseif returned[1] == "uiclick" then
					print "uiclick"
					if returned[2] then
						print "returned"
						if returned[2]:IsA("GuiButton") then
							print "2 is guibutton"
							returned[2].MouseButton1Click:Connect(function()
								print "activated"
								if tabl[value][3] == false then
									if tabl[value][5] then
										if tabl[value][5]() == true then
											if clickDebounce == false then
												clickDebounce = true
												print "button clicked"
												print "plr"
												wait(.15)
												script.Parent.Parent.Current.Value += 1
												tabl[value][3] = true
												wait(0.25)
												clickDebounce = false
											end
										end
									else--do the same thing
										if clickDebounce == false then
											clickDebounce = true
											print "button clicked"
											print "plr"
											wait(.15)
											script.Parent.Parent.Current.Value += 1
											tabl[value][3] = true
											wait(0.25)
											clickDebounce = false
										end
									end
								end
							end)
						elseif returned[3] then
						--	print(returned[3])
							local UIbutton = returned[2]:WaitForChild(returned[3])
							if UIbutton ~= nil then
								print("it is not nil!")
							end
							UIbutton.MouseButton1Click:Connect(function()
								print("clicked function running")
							end)
							UIbutton.MouseButton1Up:Connect(function()
								print("clickity clackity")
								if tabl[value][3] == false then
									if tabl[value][5] then
										if tabl[value][5]() == true then
											if clickDebounce == false then
												clickDebounce = true
												print "button clicked"
												print "plr"
												wait(.15)
												script.Parent.Parent.Current.Value += 1
												tabl[value][3] = true
												wait(0.25)
												clickDebounce = false
											end
										end
									else--do the same thing
										if clickDebounce == false then
											clickDebounce = true
											print "button clicked"
											print "plr"
											wait(.15)
											script.Parent.Parent.Current.Value += 1
											tabl[value][3] = true
											wait(0.25)
											clickDebounce = false
										end
									end
								end
							end)
						end
					end
				else
					print "none"
				end
			end
		elseif tabl[value + 1] == nil then
			script.Parent.Parent.Info.Visible = false
			script.Parent.Text = tabl[value][1]
			
			script.Parent.Parent.Teleport.Visible = true 
		end
	end
end)

wait(3)
script.Parent.Visible = true
script.Parent.Parent.Info.Visible = true
script.Parent.Parent.Current.Value = 1

None of these want to work:

UIbutton.MouseButton1Click:Connect(function()
	print("clicked function running")
end)
UIbutton.MouseButton1Up:Connect(function()
	print("clicked function running")
end)
UIbutton.MouseButton1Down:Connect(function()
	print("clicked function running")
end)
returned[2]:WaitForChild(UIbutton.Name).MouseButton1Click:Connect(function() --at this point I was trying random stuff like this
	print("clicked function running")
end)

Wait, make sure your GUI match these properties :

• GUI transparency is 0
• GUI is a ImageButton, TextButton

Otherwise, if your GUI is not a ImageButton or TextButton, you can use InputBegan function.

   frame.InputBegan:Connect(function(inputObject)

if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then

            print("Clicked!")
      end
  end)

It is a TextButton, and the BackgroundTransparency is 0. It also has UICorner, so I don’t know if that could be a problem.

I will try InputBegan though or something, if it even works.

It also seems as InputBegan doesn’t even work. Nothing is printing.

EDIT: I’m going to remove the UICorner as it might be the key to solving this.

EDIT 2: Nope.

Here is a picture of the layout if you want to see:

Screen Shot 2021-02-17 at 9.44.30 AM
Template is duplicated and it is renamed to the thing you want to buy. When you click the duplicated button it should run the function in the code, but it does not.

Is there anything in the output, when you click the button?

No, here is the whole output though:
Screen Shot 2021-02-17 at 10.21.39 AM
The last thing printed is from here:

elseif returned[1] == "uiclick" then
					print "uiclick"
					if returned[2] then
						print "returned"

Hello! Sorry for late reply but I think I solved your problem! Does your button parented in a BillBoardGui? To solve this, you should parent your BillBoardGui into the StarterGui and then put it adornee to the part that you want to attach with the BillboardGui.

I’m using a ScreenGUI, and the ScreenGUI is in StarterGui.

Did you ever find the fix? Been trying to figure out why it wont work myself.

im not a professional myself but why is the mouse-click function placed oddly inside the function that is fired every time a value changes?

just realised im 2 whole years late

Check if the button’s Active property is set to True, and make sure there aren’t any GUI objects above the button you’re trying to press.