MouseButton1Click is not being detected

Hello! I have a problem with buttons.

  1. What do you want to achieve? I want that when I click the button, it will execute a function

  2. What is the issue? Even trying with only a print(), when I click the button nothing happens, and it seems that the button click is not being detected

  3. What solutions have you tried so far? I already searched for similar topics, I tried some solutions but none of them worked. Zindex and change button position & size

image

the variable exchange is a TextButton

How can I solve this? Any help is appreciated.

You added MouseButtonClick to part?

Try this me too noob.

exchange.MouseButtonClick:Connect(function(Me)
print("AM HARE")
end)

Me()

Is there any errors in the output?
Is the exchange variable correctly tied to the button?
Is this a server script or a local script? MouseButton1Click only works on the client, so you can only use it in a local script.

No errors
Yes, the variable gets to the exchange TextButton

local menu = script.Parent.Menu
local exchange = menu:WaitForChild("ExchangeButton")

This is a LocalScript

Can I see the all code, please.

I just noticed my mistake. gave second

here

local ui = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules").UIHandler)

local menu = script.Parent.menu
local close = menu:WaitForChild("CloseButton")
local exchange = menu:WaitForChild("ExchangeButton")
local text = menu.Text
local activator = workspace.activationparts.exchange

local plr = game.Players.LocalPlayer
local ls = plr:WaitForChild("leaderstats")

local function exchange()
	wait(.01)
	print(plr.Name.. "just exchanged their blocks for coins")
	menu.Click:Play()
end

activator.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		ui.Open(script.Parent)
	end
end)


close.MouseButton1Click:Connect(function()
	menu.Click:Play()
	ui.Close(script.Parent)
end)

while wait(0.6) do
	text.Text = "Hello ".. plr.Name.."! Do you want to exchange all your blocks for ".. (ls:WaitForChild("Blocks").Value*2).. " Coins?"
end


exchange.MouseButton1Click:Connect(function()
	print("exchanged")
end)```

wait(.01) try this - wait (0.1)

The function is not triggered
the problem is there: exchange.MouseButton1Click:Connect(function() print("exchanged") end)

The exchange button doesn’t work because of the while do portion above it.
I would recommend wrapping it into a coroutine like so:

coroutine.wrap(function(functionname)
	while wait(0.6) do
		text.Text = "Hello ".. plr.Name.."! Do you want to exchange all your blocks for "..(ls:WaitForChild("Blocks").Value*2).. " Coins?"
	end
end)()

why you print - {end)‘’'} this is maybe hare Error

coroutine.wrap(function(functionname)
	while wait(0.6) do
		text.Text = "Hello ".. plr.Name.."! Do you want to exchange all your blocks for "..(ls:WaitForChild("Blocks").Value*2).. " Coins?

I tried this coroutine and also I tried to write the MouseButton1Click above the while do function, but now I got this error in the output: attempt to index function with 'MouseButton1Click'

You have a function named exchange(), try changing the name of it, or the variable exchange.

I replaced the “functionname” with “exchange” but I got the same error

That’s not what I meant. At the top of your script you have “local function exchange()”, and a variable named exchange.
Change the name of either one of them.
And in the coroutine, don’t put exchange into the parentheses, either.

The error you received is because it’s trying to do :MouseButton1Click on a function, rather than the object that you’re trying to use it on.

Oh, I changed and it works!
Thank you.

2 Likes

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