What do you want to achieve? I want that when I click the button, it will execute a function
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
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
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.
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)```
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)()
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'
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.