Script keeps adding more Beams with each click

I’m a beginner at developing scripts, and I’m trying to make an electricity and wire connector system in my game. The problem is that when I click on PCD more than once, it keeps adding more Beams, summing up each click.
Here’s the script and video:

Script:
local Part = script.Parent
local PCD = Part.ClickDetector

local WireSelection = game.Workspace:GetAttribute("SelectWireService")

local TotalEletricity = 0
local EletricityMakerPart = Part.Parent.Parent.Parent
local ViewEletricity = EletricityMakerPart.ViewEletricity.Number

local SelectingOutput = script.Parent.Parent:GetAttribute("SelectingOutput")

local MainBeamPart = EletricityMakerPart.BeamPart
local Beam = nil

local On = EletricityMakerPart:GetAttribute("On")
local IsUpdatingElectricity = false

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.InsertWire

local ServerStorage = game:GetService("ServerStorage")
local BeamType = ServerStorage.BeamTypes.EletricBeam
local CloneBeam = BeamType:Clone()

local MachineFolder = game.Workspace:WaitForChild("Machines")
local MFGetDescendants = MachineFolder:GetDescendants()

local Beam

EletricityMakerPart:GetAttributeChangedSignal("On"):Connect(function()
	On = EletricityMakerPart:GetAttribute("On")

end)

script.Parent.Parent:GetAttributeChangedSignal("SelectingOutput"):Connect(function()
	SelectingOutput = script.Parent.Parent:GetAttribute("SelectingOutput")
end)

local function updateElectricity()
	if IsUpdatingElectricity then return end

	IsUpdatingElectricity = true
	while On do
		wait(0.1)
		TotalEletricity = TotalEletricity + 1
		ViewEletricity.PlaqueText.Plaque.Text = TotalEletricity

	end
	IsUpdatingElectricity = false
end

local TService = game:GetService("TweenService")
local TInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, -1, true, 0)
local Goal = {TextTransparency = 1}

local PlaqueText = Part.Parent.PlaqueText:FindFirstChild("Plaque")
local Animation = TService:Create(PlaqueText, TInfo, Goal)

local function createNewBeam()
	local newBeam = BeamType:Clone()
	return newBeam
end

PCD.MouseClick:Connect(function(InsertWire)
	local GetInsertInputType = script.Parent.InsertInputType.Value
	if not SelectingOutput then
		SelectingOutput = true
		RemoteEvent:FireClient(InsertWire, SelectingOutput, GetInsertInputType)
		if SelectingOutput then
			task.spawn(updateElectricity)
		end
	else
		SelectingOutput = false
		RemoteEvent:FireClient(InsertWire, SelectingOutput, GetInsertInputType)
		PlaqueText.TextTransparency = 0
	end

	if SelectingOutput then
		Animation:Play()
		print("Set wire: True")
	else
		Animation:Cancel()
		print("Set wire: False")
	end

	if SelectingOutput and not On then
		for index, value in game.Workspace:GetDescendants() do
			if value:IsA("StringValue") and value.Value == "EletricInput" and value.Parent.Parent.Parent.Parent ~= EletricityMakerPart then

				print("Insert ".. value.Value)
				print("^from ".. value.Parent.Name)

				local CDFromValue = value.Parent:FindFirstChild("ClickDetector")
				if CDFromValue then
					CDFromValue.MouseClick:Connect(function()
						Beam = createNewBeam()
						Beam.Parent = value.Parent.Parent.Parent.Parent.BeamPart.Attachment
						Beam.Attachment0 = Beam.Parent
						Beam.Attachment1 = MainBeamPart.Attachment

						if Beam then

							On = true
							SelectingOutput = false
							RemoteEvent:FireClient(InsertWire, SelectingOutput)
							Animation:Cancel()
							PlaqueText.TextTransparency = 0	

							if On then
								task.spawn(updateElectricity)
							end
						end
					end)
				end				
			end
		end
	end

	if On then
		On = false
		if Beam then
			Beam:Destroy()
		end
	end
end)

Video

If you can help, I’d really appreciate it!

use :Disconnect() on the script where it got clicked you can probably research how to use it

It wouldn’t work, because I want the player to be able to disconnect and reconnect the wire.

I’m pretty sure he means to put the disconnect into the CDFromValue.MouseClick:Connect(function() function, this is where your problem is. Every time you click the input this begins to register one more time

I did this:

Script
local Part = script.Parent
local PCD = Part.ClickDetector

local WireSelection = game.Workspace:GetAttribute("SelectWireService")

local TotalEletricity = 0
local EletricityMakerPart = Part.Parent.Parent.Parent
local ViewEletricity = EletricityMakerPart.ViewEletricity.Number

local SelectingOutput = script.Parent.Parent:GetAttribute("SelectingOutput")

local MainBeamPart = EletricityMakerPart.BeamPart
local Beam = nil

local On = EletricityMakerPart:GetAttribute("On")
local IsUpdatingElectricity = false

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.InsertWire

local ServerStorage = game:GetService("ServerStorage")
local BeamType = ServerStorage.BeamTypes.EletricBeam
local CloneBeam = BeamType:Clone()

local MachineFolder = game.Workspace:WaitForChild("Machines")
local MFGetDescendants = MachineFolder:GetDescendants()

local Beam
local CDFromValueFunction -- Change

EletricityMakerPart:GetAttributeChangedSignal("On"):Connect(function()
	On = EletricityMakerPart:GetAttribute("On")

end)

script.Parent.Parent:GetAttributeChangedSignal("SelectingOutput"):Connect(function()
	SelectingOutput = script.Parent.Parent:GetAttribute("SelectingOutput")
end)

local function updateElectricity()
	if IsUpdatingElectricity then return end

	IsUpdatingElectricity = true
	while On do
		wait(0.1)
		TotalEletricity = TotalEletricity + 1
		ViewEletricity.PlaqueText.Plaque.Text = TotalEletricity

	end
	IsUpdatingElectricity = false
end

local TService = game:GetService("TweenService")
local TInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, -1, true, 0)
local Goal = {TextTransparency = 1}

local PlaqueText = Part.Parent.PlaqueText:FindFirstChild("Plaque")
local Animation = TService:Create(PlaqueText, TInfo, Goal)

local function createNewBeam()
	local newBeam = BeamType:Clone()
	return newBeam
end

PCD.MouseClick:Connect(function(InsertWire)
	local GetInsertInputType = script.Parent.InsertInputType.Value
	if not SelectingOutput then
		SelectingOutput = true
		RemoteEvent:FireClient(InsertWire, SelectingOutput, GetInsertInputType)
		if SelectingOutput then
			task.spawn(updateElectricity)
		end
	else
		SelectingOutput = false
		RemoteEvent:FireClient(InsertWire, SelectingOutput, GetInsertInputType)
		PlaqueText.TextTransparency = 0
	end

	if SelectingOutput then
		Animation:Play()
		print("Set wire: True")
	else
		Animation:Cancel()
		print("Set wire: False")
	end

	if SelectingOutput and not On then
		for index, value in game.Workspace:GetDescendants() do
			if value:IsA("StringValue") and value.Value == "EletricInput" and value.Parent.Parent.Parent.Parent ~= EletricityMakerPart then

				print("Insert ".. value.Value)
				print("^from ".. value.Parent.Name)

				local CDFromValue = value.Parent:FindFirstChild("ClickDetector") -- change
				if CDFromValue then
					CDFromValueFunction = CDFromValue.MouseClick:Connect(function()
						Beam = createNewBeam()
						Beam.Parent = value.Parent.Parent.Parent.Parent.BeamPart.Attachment
						Beam.Attachment0 = Beam.Parent
						Beam.Attachment1 = MainBeamPart.Attachment

						if Beam then

							On = true
							SelectingOutput = false
							RemoteEvent:FireClient(InsertWire, SelectingOutput)
							Animation:Cancel()
							PlaqueText.TextTransparency = 0	

							if On then
								task.spawn(updateElectricity)
							end
						end
					end)
				end				
			end
		end
	end

	if On then
		On = false
		if Beam then
			Beam:Destroy()
			CDFromValueFunction:Disconnect() --Change
		end
	end
end)

I probably wasn’t able to put into practice exactly what you said. Since it only works once (because of Disconnect()), and as I said, I wanted the player to be able to connect, disconnect and reconnect the wire.

In video 1, it keeps adding more Beams. As before. (I cancel SelectingOutput)

Video 1

In video 2, I don’t cancel SelectingOutput

Video 2

If you could explain a little bit more how to do this, i’d appreciate it :]

ok so bassically do this when you click it

clickdc = clicked:connect.MouseButton1:Connected(Function()

end)

clickdc:Disconnect()

make sure to disconnect after you call it

1 Like

That’s what I did, and that’s what happened in video 1

Try using :Once() Instead of :Connect() for situations like this

local Part = script.Parent
local PCD = Part.ClickDetector

local WireSelection = game.Workspace:GetAttribute("SelectWireService")

local TotalEletricity = 0
local EletricityMakerPart = Part.Parent.Parent.Parent
local ViewEletricity = EletricityMakerPart.ViewEletricity.Number

local SelectingOutput = script.Parent.Parent:GetAttribute("SelectingOutput")

local MainBeamPart = EletricityMakerPart.BeamPart
local Beam = nil

local On = EletricityMakerPart:GetAttribute("On")
local IsUpdatingElectricity = false

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage.InsertWire

local ServerStorage = game:GetService("ServerStorage")
local BeamType = ServerStorage.BeamTypes.EletricBeam
local CloneBeam = BeamType:Clone()

local MachineFolder = game.Workspace:WaitForChild("Machines")
local MFGetDescendants = MachineFolder:GetDescendants()

local Beam
local CDFromValueFunction -- Change

EletricityMakerPart:GetAttributeChangedSignal("On"):Connect(function()
	On = EletricityMakerPart:GetAttribute("On")

end)

script.Parent.Parent:GetAttributeChangedSignal("SelectingOutput"):Connect(function()
	SelectingOutput = script.Parent.Parent:GetAttribute("SelectingOutput")
end)

local function updateElectricity()
	if IsUpdatingElectricity then return end

	IsUpdatingElectricity = true
	while On do
		wait(0.1)
		TotalEletricity = TotalEletricity + 1
		ViewEletricity.PlaqueText.Plaque.Text = TotalEletricity

	end
	IsUpdatingElectricity = false
end

local TService = game:GetService("TweenService")
local TInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, -1, true, 0)
local Goal = {TextTransparency = 1}

local PlaqueText = Part.Parent.PlaqueText:FindFirstChild("Plaque")
local Animation = TService:Create(PlaqueText, TInfo, Goal)

local function createNewBeam()
	local newBeam = BeamType:Clone()
	return newBeam
end

PCD.MouseClick:Connect(function(InsertWire)
	local GetInsertInputType = script.Parent.InsertInputType.Value
	if not SelectingOutput then
		SelectingOutput = true
		RemoteEvent:FireClient(InsertWire, SelectingOutput, GetInsertInputType)
		if SelectingOutput then
			task.spawn(updateElectricity)
		end
	else
		SelectingOutput = false
		RemoteEvent:FireClient(InsertWire, SelectingOutput, GetInsertInputType)
		PlaqueText.TextTransparency = 0
	end

	if SelectingOutput then
		Animation:Play()
		print("Set wire: True")
	else
		Animation:Cancel()
		print("Set wire: False")
	end

	if SelectingOutput and not On then
		for index, value in game.Workspace:GetDescendants() do
			if value:IsA("StringValue") and value.Value == "EletricInput" and value.Parent.Parent.Parent.Parent ~= EletricityMakerPart then

				print("Insert ".. value.Value)
				print("^from ".. value.Parent.Name)

				local CDFromValue = value.Parent:FindFirstChild("ClickDetector") -- change
				if CDFromValue then
				 
   CDFromValue.MouseClick:Once(function() -- change!!
						Beam = createNewBeam()
						Beam.Parent = value.Parent.Parent.Parent.Parent.BeamPart.Attachment
						Beam.Attachment0 = Beam.Parent
						Beam.Attachment1 = MainBeamPart.Attachment

						if Beam then

							On = true
							SelectingOutput = false
							RemoteEvent:FireClient(InsertWire, SelectingOutput)
							Animation:Cancel()
							PlaqueText.TextTransparency = 0	

							if On then
								task.spawn(updateElectricity)
							end
						end
					end)
				end				
			end
		end
	end

	if On then
		On = false
		if Beam then
			Beam:Destroy()
		end
	end
end)

:Once() is basically :Connect(), EXCEPT it only works once… unless when the :Once() function is called/created again.

1 Like

It did work! Just made some changes, but, what u already did, was enough. Tyy very much :]

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