Why does my MouseButton1Down fires more than once?

The issue: MouseButton1Down fires +1 more time every time I enable/disable the parent frame.
At the beginning, it only fires once. However, the more I click Toggle Button, it fires 1 more time than before. Why is this?

For example, when I first clicked the Toggle button (which makes it visible), then I click the Rate Button. The Rate button will only fire once. Then, I click Toggle button again (which makes it invisible).

Next time, when I click Toggle button again (to make it visible again), then I click the Rate Button. The Rate button fires twice. Next time I keep clicking this Toggle button, the Rate button keeps firing more and more.

The script is below (I cut some parts of it so it’s easier to see):

local toggle = script.Parent.Menu.Toggle
local profileFrame = script.Parent.Profile
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

function launchRatingRequest(targetPlayer)
	if targetPlayer ~= player then
		print(player.Name.." is requesting to rate "..targetPlayer.Name..".")
		profileFrame.Visible = false
		launchRatingPrompt(targetPlayer)
	else
		print(player.Name.." attempted to rate self.")
	end
end

function launchProfile(targetPlayer)
	if profileFrame.Visible == false then
	print(player.Name.." is viewing the profile of "..targetPlayer.Name..".")
		if targetPlayer == player then profileFrame.RateButton.BackgroundColor3 = Color3.fromRGB(80,80,80) end
		profileFrame.Visible = true
		if targetPlayer == player then profileFrame.Title.Text = "My Profile" else profileFrame.Title.Text = targetPlayer.Name.."'s Profile" end
		profileFrame.Avatar.Image = PS:GetUserThumbnailAsync(targetPlayer.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
		profileFrame.Parent.Profile.Username.Text = "Name: "..targetPlayer.Name
		profileFrame.Parent.Profile.RatingStatus.Text = "Rating Status: ".."GOOD"
		
		profileFrame.RateButton.MouseButton1Down:Connect(function()
			launchRatingRequest(targetPlayer)
		end)

	elseif profileFrame.Visible == true then
		profileFrame.Visible = false
		profileFrame.RateButton.BackgroundColor3 = Color3.fromRGB(130, 222, 113)
	end
end
		
toggle.MouseButton1Click:Connect(function()
	launchProfile(player)
end)```

Do you have a mouse that double clicks? Cause’ some mice double clicks like gaming mouses

1 Like

The problem is that you are connecting a MouseButton1Down event on the RateButton every time the toggle button is clicked. You can instead connect the event outside the launchProfile function.

1 Like

I’m pretty sure this is not an issue relating to Mouse, it is about the script.

Can you please explain to me what you meant?

Every time you toggle the frame, you fire the launchProfile function and inside that function you have a MouseButton1Down event for the RateButton. So this means that every time you toggle the frame a new MouseButton1Down event will be created which will not overwrite the previous one.

function launchProfile(targetPlayer)
	if profileFrame.Visible == false then
	print(player.Name.." is viewing the profile of "..targetPlayer.Name..".")
		if targetPlayer == player then profileFrame.RateButton.BackgroundColor3 = Color3.fromRGB(80,80,80) end
		profileFrame.Visible = true
		if targetPlayer == player then profileFrame.Title.Text = "My Profile" else profileFrame.Title.Text = targetPlayer.Name.."'s Profile" end
		profileFrame.Avatar.Image = PS:GetUserThumbnailAsync(targetPlayer.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
		profileFrame.Parent.Profile.Username.Text = "Name: "..targetPlayer.Name
		profileFrame.Parent.Profile.RatingStatus.Text = "Rating Status: ".."GOOD"
		
		profileFrame.RateButton.MouseButton1Down:Connect(function() -- < Here
			launchRatingRequest(targetPlayer)
		end)

	elseif profileFrame.Visible == true then
		profileFrame.Visible = false
		profileFrame.RateButton.BackgroundColor3 = Color3.fromRGB(130, 222, 113)
	end
end

You can instead try putting this below the toggle mouse click event.

profileFrame.RateButton.MouseButton1Down:Connect(function()
	launchRatingRequest(targetPlayer)
end)
1 Like

The issue that would come from that is the “targetPlayer” will be undefined.

Just replace targetPlayer with player since it’s a local script.

The targetplayer is somebody else, not the local player.

targetPlayer is the local player since whenever you toggle the frame, you are passing the player argument to the function. There cannot be two different players on one client.

Oh, it’s not included in script above (since I cut it for easier viewing purposes).

Yes, in my case targetPlayer can be localplayer or somebody else because I have a profile system where players can view their own profile or view other people’s profile by clicking a body part.

I’ve found a solution where I just made another currentTargetPlayer variable at the beginning of the script. So the currentTargetPlayer can be accessed anywhere, not only local to the function.

Thanks for helping identify the problem!

1 Like