How to check when player is not clicking a gui button?

I am trying to stop sound when a player stops clicking the gui button
Here’s what I have tried it isn’t working though cause when I click on the button it keeps firing remote events both of them I only want it to send the Sound remote event when button is being clicked. And send SoundStop remote event when button is not being clicked.


local Button = script.Parent

local Clicked = false

Button.MouseButton1Down:Connect(function(plr)
	Clicked = true

	while Clicked do
		game.ReplicatedStorage.Sound:FireServer(plr)
		wait(.2)
	end
	game.ReplicatedStorage.Soundstop:FireServer(plr)
end)

Button.MouseButton1Up:Connect(function(plr)
	Clicked = false
end)
1 Like

Try changing while Clicked do to while Clicked == true do, as from my knowledge just having it set as while Clicked do will continue the loop for as long as Clicked ~= nil

I have just tried what you had suggestion it didn’t make any difference sadly

local Button = script.Parent

local Clicked = false

Button.MouseButton1Down:Connect(function(plr)
	Clicked = true
	while Clicked do
		game.ReplicatedStorage.Sound:FireServer(plr)
		task.wait(.2)
	end
end)

Button.MouseButton1Up:Connect(function(plr)
	Clicked = false
	game.ReplicatedStorage.Soundstop:FireServer(plr) --Stop the sound after clicked is false
	print("Sound stop attempt")
end)

if you take game.ReplicatedStorage.Sound:FireServer(plr) out of the loop then it will only run once.

local Button = script.Parent

local Clicked = false

Button.MouseButton1Down:Connect(function(plr)
	Clicked = true

	game.ReplicatedStorage.Sound:FireServer(plr)
	while Clicked do
		wait(.2)
	end
	game.ReplicatedStorage.Soundstop:FireServer(plr)
end)

Button.MouseButton1Up:Connect(function(plr)
	Clicked = false
end)
2 Likes

Thank you it worked!!
Sorry for late respond.

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