Why does this script make more welds everytime the event is fired

so i have a script that lets me make a road to grambys kinda building system. although for some reason when i take a part off and put it back on, it makes more and more welds.
i put it on the first time, 1 weld(good).
i put it on again, 2 welds
again, 4 welds
again, 8 welds…

idk whats going on, heres the part of the script responsible for handling this:

--Nodes
local Nodes = script.Parent.Nodes

local Tank = Nodes.Tank
local RBL = Nodes.RightBackLight
local LBL = Nodes.LeftBackLight
local Engine = Nodes.Engine
local Battery = Nodes.Battery
local FLL = Nodes.FrontLeftLight
local FRL = Nodes.FrontRightLight

--Other Values
local objselected = nil
local PlaceSender = game.ReplicatedStorage:WaitForChild("PlaceSender")
local PlaceReciver = game.ReplicatedStorage:WaitForChild("PlaceReciver")

--Node Sender
PlaceSender.Event:Connect(function(Param, Type)
	print("Started")
	if Param == "Ready" then
		
		
		
		if Tank:FindFirstChild("Weld") then
			print("Removing weld!")
			Tank.Weld:Destroy()
		end
		Tank.ClickDetector.MouseClick:Connect(function(plr)
			plr.ObjSelected.Value = false
			
			local Weld = Instance.new("Weld")
			Weld.Parent = Tank
			Weld.Part0 = Tank
			Weld.Part1 = Type.PlacementPart
			print("Moved"..Type.Name.."To Tank!")
		end)
		
		
		
		if RBL:FindFirstChild("Weld") then
			print("Removing weld!")
			RBL.Weld:Destroy()
		end
		RBL.ClickDetector.MouseClick:Connect(function(plr)
			plr.ObjSelected.Value = false

			local Weld = Instance.new("Weld")
			Weld.Parent = RBL
			Weld.Part0 = RBL
			Weld.Part1 = Type.PlacementPart
			print("Moved"..Type.Name.."To RightBackLight!")
		end)
		
		
		
		if LBL:FindFirstChild("Weld") then
			print("Removing weld!")
			LBL.Weld:Destroy()
		end
		LBL.ClickDetector.MouseClick:Connect(function(plr)
			plr.ObjSelected.Value = false

			local Weld = Instance.new("Weld")
			Weld.Parent = LBL
			Weld.Part0 = LBL
			Weld.Part1 = Type.PlacementPart
			print("Moved"..Type.Name.."To LeftBackLight!")
		end)
		
		
		
		if Engine:FindFirstChild("Weld") then
			print("Removing weld!")
			Engine.Weld:Destroy()
		end
		Engine.ClickDetector.MouseClick:Connect(function(plr)
			plr.ObjSelected.Value = false

			local Weld = Instance.new("Weld")
			Weld.Parent = Engine
			Weld.Part0 = Engine
			Weld.Part1 = Type.PlacementPart
			print("Moved"..Type.Name.."To Engine!")
		end)
		
		
		
		if Battery:FindFirstChild("Weld") then
			print("Removing weld!")
			Battery.Weld:Destroy()
		end
		Battery.ClickDetector.MouseClick:Connect(function(plr)
			plr.ObjSelected.Value = false

			local Weld = Instance.new("Weld")
			Weld.Parent = Battery
			Weld.Part0 = Battery
			Weld.Part1 = Type.PlacementPart
			print("Moved"..Type.Name.."To Battery!")
		end)
		
		
		
		if FLL:FindFirstChild("Weld") then
			print("Removing weld!")
			FLL.Weld:Destroy()
		end
		FLL.ClickDetector.MouseClick:Connect(function(plr)
			plr.ObjSelected.Value = false

			local Weld = Instance.new("Weld")
			Weld.Parent = FLL
			Weld.Part0 = FLL
			Weld.Part1 = Type.PlacementPart
			print("Moved"..Type.Name.."To FrontLeftLight!")
		end)
		
		
		
		if FRL:FindFirstChild("Weld") then
			print("Removing weld!")
			FRL.Weld:Destroy()
		end
		FRL.ClickDetector.MouseClick:Connect(function(plr)
			plr.ObjSelected.Value = false

			local Weld = Instance.new("Weld")
			Weld.Parent = FRL
			Weld.Part0 = FRL
			Weld.Part1 = Type.PlacementPart
			print("Moved"..Type.Name.."To FrontRightLight!")
		end)
		
		
		
		
		
		
	end
end)

(please ignore my bad code, im not very good)

Thanks for any help!

1 Like

Are you sure the Removing weld! even prints? You might want to move that inside the MouseClick events, no?

You are connecting a new event every time the event runs, you might want to use :Once() instead of :Connect()

i checked, and it DOES remove the weld, it seemingly does everything properly, it changes my player value, everything… it just keeps making more welds

where do u want me to do once? i tried it with the

Engine.ClickDetector.MouseClick:Once(function(plr)
			plr.ObjSelected.Value = false

			local Weld = Instance.new("Weld")
			Weld.Parent = Engine
			Weld.Part0 = Engine
			Weld.Part1 = Type.PlacementPart
			print("Moved"..Type.Name.."To Engine!")
		end)

ect, but it still does the dupe thing
(by ect i mean i did it with all the functions inside the event function)

Yeah your logic is kinda messed up, since your events don’t disconnect when you are not using them, and just reconnecting new ones, so either rethink your entire logic of handling the placement, or you could just save the events in a table

events["FRL"] = FRL.ClickDetector.MouseClick:Connect(...)

like this for every single one, and in each event you will have to go through the events table and disconnect each event

-- at the bottom of each MouseClick event:
for _,v in events do
    if typeof(v) == "RBXScriptConnection" then v:Disconnect() end
end

im not sure what you mean, idk how to use tables

Change it like this for every single event:

FRL.ClickDetector.MouseClick:Connect(function(plr)

to

events["FRL"] = FRL.ClickDetector.MouseClick:Connect(function(plr)

what is “events”? i dont have a variable or anything called that… is this a table thing?
(ive never used tables on my own so idk what im doing)

yes, it’s a table

local events = {}

at the top of ur code (outside of the PlaceSender.Event function)

what do i put in the bracket?
chat limittttttt

nothing, it’s an empty table, you assign data to it the way i told you earlier

events["FRL"] = FRL.ClickDetector.MouseClick:Connect(function(plr)

is that all or do i also put the

    if typeof(v) == "RBXScriptConnection" then v:Disconnect() end
end

You have to put this at the bottom of each MouseClick event:

for _,v in events do
    if typeof(v) == "RBXScriptConnection" then v:Disconnect() end
end

as in past the “end)” or like the end of the function

You’ll still need end) after it to close the event’s scope.

i just tried that as u sent the message, that worked! thank you so much, and thanks for being patient with me

1 Like

Wait now im having another issue, now when i place more than 1 part on the car, when i take one off it breaks all the welds
Wait i may have a fix for that

it breaks all the welds

yeah assuming that the PlaceSender.Event event is getting fired, it destroys each weld. (Possible solution to this is doing what i’ve suggested in my first message):

You might want to move that inside the MouseClick events, no?

1 Like

im trying that, i forgot you said that lol

ok well im dumb, i just put the weld break scripts AFTER the weld make scripts… lemme just fix that…