For loop connects ProximityPrompt.Triggered:Connect() Twice

i was making a battery system, and once i made the battery collection system, the problem started

i made a for loop to detect all tagged batteries in CollectionService

but for some reason, the for repeats twice

i mean, it connects the Triggered event twice


i tried to make a debounce value, so if its true, it will not fire again, it didn,t work

i also tried to put each existing instance in a table, and if it exists in the table, it will not fire again, it didn,t work neither

i also tried to put a wait, didn,t work neither


here’s my whole script

local TweenService = game.TweenService
local TweenTime = 0.1
local TweenStyle = Enum.EasingStyle.Sine
local TweenDirection = Enum.EasingDirection.InOut

local Plr = game.Players.LocalPlayer

local BatteryGui = Plr.PlayerGui:WaitForChild("BatteryGui")
local BatteryBar = BatteryGui.Battery.BatteryCamp.BatteryBar
local BatteryStoredText = BatteryGui.Battery.BatteryStored

local BatteryStored = script.BatteryStored

local WaitTime = 0.1

local Count = WaitTime

local ChargeLimit = 100
local DrainEvent = script.Drain
local NotDrainEvent = script.StopDraining

local CanRepeat = true

local FlashlightName = "Flashlight"

if BatteryGui then

	function UpdateBatteryStorageDisplay()
		BatteryStoredText.Text = tostring(BatteryStored.Value)
	end

	function RechargeBattery(Flashlight)
		if BatteryStored.Value > 0 then

			local Percentage = Flashlight:FindFirstChild("Percentage")

			Flashlight.Percentage.Value = ChargeLimit

			BatteryStored.Value -= 1

			TweenService:Create(BatteryBar, TweenInfo.new(TweenTime, TweenStyle, TweenDirection), {Size = UDim2.fromScale(1, 1)}):Play()
			UpdateBatteryStorageDisplay()
		else
			Flashlight:FindFirstChild("LocalScript").TurnOff:Fire()
		end
	end


	function BatteryCollected(Battery)
		print(1)

		BatteryStored.Value += 1

		script.PickupSound:Play()
		local Flashlight = Plr.Character:FindFirstChild(FlashlightName) or Plr.Backpack:FindFirstChild(FlashlightName)

		Flashlight:FindFirstChild("LocalScript").Recharge:Fire()
		UpdateBatteryStorageDisplay()
	end

	function DrainBattery(Flashlight)		
		CanRepeat = true
		while CanRepeat == true do
			BatteryGui.Battery.ConsumeDetector.Visible = true
			wait(Count)
			Count = WaitTime

			local Percentage = Flashlight:FindFirstChild("Percentage")

			if Flashlight.Parent ~= Plr.Backpack then
				if Percentage.Value > 0 then
					Percentage.Value -= 1

					local ToSize = Percentage.Value/ChargeLimit

					TweenService:Create(BatteryBar, TweenInfo.new(TweenTime, TweenStyle, TweenDirection), {Size = UDim2.fromScale(ToSize, 1)}):Play()



				else
					RechargeBattery(Flashlight)

				end
			end
		end
		BatteryGui.Battery.ConsumeDetector.Visible = false
	end

	function PauseDraining()
		CanRepeat = false
	end

	DrainEvent.Event:Connect(DrainBattery)
	NotDrainEvent.Event:Connect(PauseDraining)
end



local CollectionSvc = game:GetService("CollectionService")

for i, Battery in pairs(CollectionSvc:GetTagged("Batteries")) do
	Battery.Part.Collect.Triggered:Connect(function()
		BatteryCollected(Battery)
	end)
end

CollectionSvc:GetInstanceAddedSignal("Batteries"):Connect(function(Battery)
	Battery.Part.Collect.Triggered:Connect(function()
		BatteryCollected(Battery)
	end)
end)

there’s another solution that i can try? i can,t think in another anymore, i tried everything that i know

Yeah, this is weird, this is running serially and this shouldn’t occur.

Try adding some prints to see if the battery is somehow triggering the GetInstanceAddedSignal.

for i, Battery in pairs(CollectionSvc:GetTagged("Batteries")) do
    print("for loop battery")
	Battery.Part.Collect.Triggered:Connect(function()
		BatteryCollected(Battery)
        print("for loop battery triggered")
	end)
end

CollectionSvc:GetInstanceAddedSignal("Batteries"):Connect(function(Battery)
    print("connection battery")
	Battery.Part.Collect.Triggered:Connect(function()
		BatteryCollected(Battery)
        print("connection battery triggered")
	end)
end)

Let me know what it outputs. If it outputs both for one battery, that’s our problem.

Yes it does

‘for loop battery’ and ‘for loop’ printed twice

i tried to duplicate the model while playtesting and it printed twice in :GetInstanceAddedSignal(“Batteries”) too, i think the problem is in both, but in separate ways (if i pronunce right)

It was hard but i managed to find the solution

so, the problem is not in the script itself and its where the script is located. before i discovered the problem, the script was located in StarterPlayerScripts instead, making the code repeat twice.


in theory of why this happen is because the script clones itself to move to PlayerScripts Instance, once a player joined.

and since the script in StarterPlayerScripts was already executing, the script itself and the cloned script runs twice. mading two scripts with same behavior execute

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