Module runnign more then once

so i have a part with a click detector and when I press the par the module runs, however the module runs more then once sometimes 2 times sometimes 4 times, I added a debounce to the click detector script so I don’t think that’s the issue

local module = {}
local amt = 0
local debounce = false
module.skillgiver = function(player,SkillName)
	local rs = game:GetService("ReplicatedStorage")
	for i,v in player.Backpack:GetChildren() do
		if v.Name == SkillName then
			for i,s in player.PlayerGui.keybindFrame.Frame:GetChildren() do
				if s:IsA("TextLabel") and s.Taken.Value == false and debounce == false then
					local toolui = rs.TextLabel
					local UiClone = toolui:Clone()
					debounce = true
					
					UiClone.Parent = player.PlayerGui.HotbarFrame.Frame
					UiClone.Position = s.Position
					s.Taken.Value = true
					amt = amt + 1
					debounce = false
					print(amt)
					break
				elseif s:IsA("TextLabel") and s.Taken.Value == true and amt < 10 and debounce == false then
					debounce = true
					amt = amt +1
					debounce = false
				elseif s:IsA("TextLabel") and s.Taken.Value == true and amt >= 10 and debounce == false then
					debounce = true
					local toolui = rs.TextLabel
					local UiClone = toolui:Clone()


					UiClone.Parent = player.PlayerGui.Inventory.ScrollingFrame
					UiClone.TextLabel.Visible = false
					debounce = false
				end
			end
		else
			print("EASY")
		end
	end
end


return module

Mind showing the place where you use this module?

here it is

local click = script.Parent.ClickDetector
local module = require(game:GetService("ReplicatedStorage").ModuleScript)
local debounce = false

click.MouseClick:Connect(function(plr)
	if debounce == false then
		debounce = true
		module.skillgiver(plr,"GURA")
		task.wait(1)
		debounce = false
	end
end)

Unless you have race conditions by having multiple click detections across different scripts, I think it could be because of the nested iterations within the first script you posted.

What’s happening more than once by the way? The print statement?

the amt value, i want it to increase by 1 every time a specific condition is met but sometimes it increases by like 4

You are iterating through every object and incrementing amt for each one that satisfies the conditional.

Consider breaking the loop once a match (which then increments amt) is found.

3 Likes

hey, ik this is a bit let but could u elaborate more?
i added a break after, it still didn’t take work

local module = {}
local amt = 0
local debounce = false
module.skillgiver = function(player,SkillName)
	local rs = game:GetService("ReplicatedStorage")
	for i,v in player.Backpack:GetChildren() do
		if v.Name == SkillName then
			for i,s in player.PlayerGui.keybindFrame.Frame:GetChildren() do
				if s:IsA("TextLabel") and s.Taken.Value == false and debounce == false then
					local toolui = rs.TextLabel
					local UiClone = toolui:Clone()
					debounce = true
					
					UiClone.Parent = player.PlayerGui.HotbarFrame.Frame
					UiClone.Position = s.Position
					s.Taken.Value = true
					amt = amt + 1
					debounce = false
					print(amt)
			break
				elseif s:IsA("TextLabel") and s.Taken.Value == true and amt < 10 and debounce == false then
					debounce = true
					amt = amt +1
					debounce = false
				elseif s:IsA("TextLabel") and s.Taken.Value == true and amt >= 10 and debounce == false then
					debounce = true
					local toolui = rs.TextLabel
					local UiClone = toolui:Clone()


					UiClone.Parent = player.PlayerGui.Inventory.ScrollingFrame
					UiClone.TextLabel.Visible = false
					debounce = false
				end
			end
		else
			print("EASY")
		end
	end
end


return module

Hi! I have overviewed your code, and i think i have found your issue.

Your break statement does break the for loop you are using to iterate over the UI elements, but the loop that iterates over your tools keeps running, thus causing amt to be further added to. I have added a few lines that check if amt is ever changed, and if so, breaks the upper loop.

Hope this helps!

local rs = game:GetService("ReplicatedStorage")

local module = {}
local amt = 0
local debounce = false

module.skillgiver = function(player,SkillName)
	--// Save the old amount at the time of function call, so we can track changes
	local OldAmount = amt
	
	for _, v in player.Backpack:GetChildren() do
		--// Here we check if the value has changed. If changed, we break the main loop
		if amt ~= OldAmount then
			break
		end
		
		if v.Name == SkillName then
			for _,s in player.PlayerGui.keybindFrame.Frame:GetChildren() do
				if s:IsA("TextLabel") and s.Taken.Value == false and debounce == false then
					local toolui = rs.TextLabel
					local UiClone = toolui:Clone()
					debounce = true

					UiClone.Parent = player.PlayerGui.HotbarFrame.Frame
					UiClone.Position = s.Position
					s.Taken.Value = true
					
					amt = amt + 1
					
					debounce = false
					
					break
				elseif s:IsA("TextLabel") and s.Taken.Value == true and amt < 10 and debounce == false then
					debounce = true
					amt = amt +1
					debounce = false
				elseif s:IsA("TextLabel") and s.Taken.Value == true and amt >= 10 and debounce == false then
					debounce = true
					local toolui = rs.TextLabel
					local UiClone = toolui:Clone()


					UiClone.Parent = player.PlayerGui.Inventory.ScrollingFrame
					UiClone.TextLabel.Visible = false
					debounce = false
				end
			end
		else
			print("EASY")
		end
	end
end

return module```

Hey thanks for the help, however the issue still presists, i tried printing the oldammount and running the module a couple of times here is what it prints

image
it goes from 0 to 1 which is an increase of 1 then 1 to 3 which is 2 then 3 to 6 which is 3, then when it reaches 10 it goes up by 1 like intended

Hi, i have spotted the other issue. The other condition where you are doing amt = amt + 1 also doesn’t have a break statement (im surprised that i missed that too), simply adding that should resolve the issue fully.

				elseif s:IsA("TextLabel") and s.Taken.Value == true and amt < 10 and debounce == false then
					debounce = true
					amt = amt +1
					debounce = false
					break
1 Like

this has solved the ammount issue, however after the break runs it complete stops the for loop is there anyway i can just stop the specific loop then make it move to the next child? like i want it to continute till Taken value = false and amt is less then 10