A problem with a for loop

So i made a part which heals you when you touch it, then created like 5 copies, but the problem is when i touch one of the health parts, the other parts get disabled too,they cant heal you until the cooldown is finished
any ideas?

The Code:

--Variables
local player = game:GetService("Players")
local maxhealth = 100
local healthpickup = game.Workspace.Health:GetChildren() -- health is the model that contains the parts
local cooldown = 5
local disabledTransparency = 0.8
local enabledTransparency = 0.4
local debounce = true

--Functions

for _, healthpickup in pairs(healthpickup) do
	healthpickup.Touched:Connect(function(HumanParts,Health)
		local chr = HumanParts.Parent
		local humanoid = chr:FindFirstChildWhichIsA("Humanoid")
		if debounce then
			if humanoid then
				debounce = false
				humanoid.Health = maxhealth
				healthpickup.Transparency = disabledTransparency
				wait(10)
				healthpickup.Transparency = enabledTransparency
				debounce = true
			end
		end
	end)
end

move the debounce into the for loop.

--Variables
local player = game:GetService("Players")
local maxhealth = 100
local healthpickup = game.Workspace.Health:GetChildren() -- health is the model that contains the parts
local cooldown = 5
local disabledTransparency = 0.8
local enabledTransparency = 0.4

--Functions

for _, healthpickup in pairs(healthpickup) do
	local debounce = true
	healthpickup.Touched:Connect(function(HumanParts,Health)
		local chr = HumanParts.Parent
		local humanoid = chr:FindFirstChildWhichIsA("Humanoid")
		if debounce then
			if humanoid then
				debounce = false
				humanoid.Health = maxhealth
				healthpickup.Transparency = disabledTransparency
				wait(10)
				healthpickup.Transparency = enabledTransparency
				debounce = true
			end
		end
	end)
end
1 Like
local Players = game:GetService("Players")
local HealthFolder = workspace:WaitForChild("Health")
local Debounce = true

for _, HealthPickup in ipairs(HealthFolder:GetChildren()) do
	HealthPickup.Touched:Connect(function(Hit)
		if Debounce then
			return
		end

		local Player = Players:GetPlayerFromCharacter(Hit.Parent)
		if Player then
			Debounce = true
			local Character = Hit.Parent
			local Humanoid = Character.Humanoid
			Humanoid.Health = Humanoid.MaxHealth
			HealthPickup.Transparency = 0.8
			task.wait(5)
			HealthPickup.Transparency = 0.4
			Debounce = false
		end
	end)
end

It is possible to have a separate debounce for each health pickup and for each player, if that is necessary let me know.

1 Like

yes that will be much appreciated!
Thanks for the help!

local Players = game:GetService("Players")
local HealthFolder = workspace:WaitForChild("Health")
local Debounces = {}

for _, HealthPickup in ipairs(HealthFolder:GetChildren()) do
	table.insert(Debounces, {HealthPickup})
	HealthPickup.Touched:Connect(function(Hit)
		local Player = Players:GetPlayerFromCharacter(Hit.Parent)
		if Player then
			for _, Debounce in ipairs(Debounces) do
				if Debounce[1] == HealthPickup then
					local TablePlayer = table.find(Debounce, Player)
					if TablePlayer then
						return
					end
				end
			end
			
			for _, Debounce in ipairs(Debounces) do
				if Debounce[1] == HealthPickup then
					table.insert(Debounce, Player)
				end
			end
			
			table.insert(Debounces, {HealthPickup, Player})
			local Character = Hit.Parent
			local Humanoid = Character.Humanoid
			Humanoid.Health = Humanoid.MaxHealth
			HealthPickup.Transparency = 0.8
			task.wait(5)
			HealthPickup.Transparency = 0.4
			
			for _, Debounce in ipairs(Debounces) do
				if Debounce[1] == HealthPickup then
					local TablePlayer = table.find(Debounce, Player)
					if TablePlayer then
						table.remove(Debounce, TablePlayer)
					end
				end
			end
		end
	end)
end
1 Like