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