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)
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
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 :]
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.