mouse.Button1Down Triggering Multiple Times

Hello,

It appears i’m having trouble with the mouse.Button1Down:Connect function. I’m making a script to manage a placement system for a game. It works great except I have noticed that when the user places the object it repeats the function multiple times. This was fine in the early stages of development because the only effect was something being printed multiple times and occasional lag.
Then I started working on passing data from the client to the server because its all contained in a local script inside ReplicatedFirst and I need other players to see the placed part. Due to this repeating glitch or error in the code it sends the data to the server multiple times resulting in more than the desired amount of parts being placed.

if anyone knows of this issue or sees a problem in my code it would be greatly appreciated.

Problem code:

mouse.Button1Down:Connect(function()
			if placeable == false then
				part:Destroy()
				part2:Destroy()
				if towersPlaced == false then
					towersPlaced = false
				end
			else
				local tower = "Test"
				local locX = part.Position.X
				local locY = part.Position.Y
				local locZ = part.Position.Z
				print "placed"
				Clicked = true
				towersPlaced = true
				part2.Parent = game.workspace.placedTowers
				--send location and name to server script
				game.ReplicatedStorage.PlaceObject:InvokeServer(locX,locY,locZ,tower)
				part:Destroy()
				part2:Destroy()
				print(part.Position)

			end
			--wait function prevents multi placement within local script and not within server script
			wait (.0001)
		end)

Whenever I place the object it prints “placed” about 15 times instead of once

Is this a known issue? I’m moderately experienced with scripting but this is my first time embarking on a large project involving client to server interactions and similar.

If you need to know it is contained inside another mouse.Button1Down function for a TextButton.

I apologize for the messy code I’m not the most organized person but at least it works.

I can provide the rest of the code if needed its just kinda long and I dont want to overcomplicate things.

You must have connected that function to the event 15 times. There’s no other option.

How are you connecting the event? There’s no way for Button1Down to activate more than once when you press left click unless you’re connecting it more than once

Couldn’t you use a denounce script? I’m not sure,

The Button1Down even fires when the the player presses their left mouse button.
So debounce does not make any sense here.

1 Like

I’m not exactly sure what you mean but in the script I’m invoking I have this:

game.ReplicatedStorage.PlaceObject.OnServerInvoke= function(plr,locX,locY,locZ,tower)

I followed alvinblox’s tutorial on RemoteFunctions.

I dont think this has to do with the remote function though because this was a problem before implementing the server interaction

The code you posted in your first post in this topic is probably being executed multiple times, that’s why you are getting more than one connection.

I’ve looked through my script but couldnt seem to find anything that would be causing it to execute multiple times. The only possibility would be this for loop but the button1down function isnt contained inside of it. however it does have some influence because it determines if the part can be placed on the surface its touching or not

for i,v in next, part2:GetTouchingParts() do

			if (v.Parent == game.Workspace.UnplaceableAreas or v.Parent == game.workspace.placedTowers) then
				part.Color = Color3.new(1, 0, 0)
				print "dont place"
				placeable = false
			else
				part.Color = Color3.new(0, 1, 0)
				placeable = true
			end
end

Can I see the full code containing the snippet from the original post?

Heres the whole script:


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

local towersPlaced = false
local Clicked = false
local stats = require(game.ReplicatedFirst.CashManagement.Info)
stats = stats["Test"]
local PlayerGui = game:GetService('Players').LocalPlayer:WaitForChild('PlayerGui')

PlayerGui.Button.TextButton.MouseButton1Click:Connect(function()
	local part = Instance.new("Part") --create range part
	part.Parent = workspace.NoClick
	
	part.Size = Vector3.new(.2,stats.Range,stats.Range)
	part.Orientation = Vector3.new(0,0,90)
	part.Anchored = true
	part.CanCollide = false
	part.Position = Vector3.new(mouse.Hit.Position.X,mouse.Hit.Position.Y,mouse.Hit.Position.Z)
	part.Shape = Enum.PartType.Cylinder
	
	part.BackSurface = Enum.SurfaceType.Smooth
	part.FrontSurface = Enum.SurfaceType.Smooth
	part.BottomSurface = Enum.SurfaceType.Smooth
	part.TopSurface = Enum.SurfaceType.Smooth
	part.LeftSurface = Enum.SurfaceType.Smooth
	part.RightSurface = Enum.SurfaceType.Smooth
	
	part.Material = Enum.Material.Neon
	part.Transparency = .8
	part.Color = Color3.new(0, 1, 0)
	part.CanCollide = false
	
	print (mouse.Hit.Position.X)
	print (mouse.Hit.Position.Y)
	print (mouse.Hit.Position.Z)
	
	print (stats.Range)
	
	local part2 = Instance.new("Part")--create placement boundry part
	part2.Parent = workspace.NoClick

	part2.Size = Vector3.new(2,3,2)
	part2.Anchored = true
	part2.CanCollide = true
	part2.Position = Vector3.new(mouse.Hit.Position.X,mouse.Hit.Position.Y,mouse.Hit.Position.Z)

	part2.BackSurface = Enum.SurfaceType.Smooth
	part2.FrontSurface = Enum.SurfaceType.Smooth
	part2.BottomSurface = Enum.SurfaceType.Smooth
	part2.TopSurface = Enum.SurfaceType.Smooth
	part2.LeftSurface = Enum.SurfaceType.Smooth
	part2.RightSurface = Enum.SurfaceType.Smooth

	part2.Material = Enum.Material.Neon
	part2.Color = Color3.new(0.92752, 0.885801, 0)

	print (mouse.Hit.Position.X)
	print (mouse.Hit.Position.Y)
	print (mouse.Hit.Position.Z)

	print (stats.Range)

	mouse.TargetFilter = workspace.NoClick
	
	local placeable = true
	
	while Clicked == false	do
		part.Position = Vector3.new(mouse.Hit.Position.X,mouse.Hit.Position.Y+(part.Size.X/2),mouse.Hit.Position.Z)
		part2.Position = Vector3.new(part.Position.X,part.Position.Y-1,part.Position.Z)
		local isTouching = false
		placeable = false
		
		local targetPart=game.Workspace.UnplaceableAreas
		
		for i,v in next, part2:GetTouchingParts() do

			if (v.Parent == game.Workspace.UnplaceableAreas or v.Parent == game.workspace.placedTowers) then
				part.Color = Color3.new(1, 0, 0)
				print "dont place"
				placeable = false
			else
				part.Color = Color3.new(0, 1, 0)
				placeable = true
			end
		
		
			
	
		end
		mouse.Button1Down:Connect(function()
			if placeable == false then
				part:Destroy()
				part2:Destroy()
				if towersPlaced == false then
					towersPlaced = false
				end
			else
				local tower = "Test"
				local locX = part.Position.X
				local locY = part.Position.Y
				local locZ = part.Position.Z
				print "placed"
				Clicked = true
				towersPlaced = true
				part2.Parent = game.workspace.placedTowers
				--send location and name to server script
				game.ReplicatedStorage.PlaceObject:InvokeServer(locX,locY,locZ,tower)
				part:Destroy()
				part2:Destroy()
				print(part.Position)

			end
			--wait function prevents multi placement within local script and not within server script
			wait (.0001)
		end)
			wait(.0001)
	end
	
	Clicked = false
	script.Disabled = true
	wait(1)
	script.Disabled = false
	end)

Your event is in a while loop which is in another event. I’m not surprised that you connect it 15 times.

Ah, I see. Thank you for pointing it out to me. I guess i’ve overlooked that. I’ll go about finding a solution thank you.