Setting a value makes it change multiple times

  1. What do you want to achieve? Keep it simple and clear!

The script should change the amount of durability that a tool will gain when a button is clicked. Each tool has a current and max durability that are set elsewhere.

  1. What is the issue? Include screenshots / videos if possible!

Every time one of the buttons are pressed, the code will repeat +1 times. The values will change but loop through the values for every tool that has been clicked on and always end on the first tool that was clicked on resulting in the values looking like they never change.

None of my code should do any looping so I think that the gui may be overlapping somehow. I don’t know how this would happen as no new guis are being created but I don’t have any other explanation and don’t know how to fix this if it is the case.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried looking for solutions but have found none that match my problem.

Script:

local VPFHolder = script.Parent.Parent.VPFHolderBackground.VPFHolder
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local ItemFolder = char:WaitForChild("ItemFolder")

local Min = script.Parent.Min
local Max = script.Parent.Max
local Add = script.Parent.Add
local Sub = script.Parent.Sub
local AddText = script.Parent.toAdd
local toAdd = 0

VPFHolder.ChildAdded:Connect(function()
	if VPFHolder:GetChildren()[1] ~= nil then
		local ItemVPF = VPFHolder:GetChildren()[1]
		local Item = string.gsub(ItemVPF.Name, "VPF", "")

		ItemVPF.ChildAdded:Connect(function()
			local ItemNum = ItemVPF.ItemNum.Value
			local MaxDur = ItemFolder[ItemNum].MaxDur.Value
			local CurrDur = ItemFolder[ItemNum].CurrDur.Value
			toAdd = 0
			AddText.Text = "+"..toAdd
			Min.Activated:Connect(function()
				toAdd = 0
				AddText.Text = "+"..toAdd
			end)
			Sub.Activated:Connect(function()
				if CurrDur + toAdd - 1 >= CurrDur then
					toAdd -= 1
					AddText.Text = "+"..toAdd
				end
			end)
			Add.Activated:Connect(function()
				if CurrDur + toAdd + 1 <= MaxDur then
					toAdd += 1
					AddText.Text = "+"..toAdd
				end
			end)
			Max.Activated:Connect(function()
				toAdd = MaxDur - CurrDur
				AddText.Text = "+"..toAdd
			end)
		end)
	end
end)
1 Like

try adding a debounce, and making it just a wait() until its back to false.

for example:

i tried that already but nothing changed. It added a cooldown to using the button but when it was activated, it would still happen multiple times

I still have no idea what the problem was but I rewrote it like this and now it works. The original problem is interesting to me though and if anybody knows what the problem was, I would appreciate it if you let me know.

local VPFHolder = script.Parent.Parent.VPFHolderBackground.VPFHolder
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local ItemFolder = char:WaitForChild("ItemFolder")
local cra = game:GetService("ReplicatedStorage").Events.ChangeRepAmount

local Min = script.Parent.Min
local Max = script.Parent.Max
local Add = script.Parent.Add
local Sub = script.Parent.Sub
local AddText = script.Parent.toAdd
local toAdd = 0
local debounce = false

VPFHolder.ChildAdded:Connect(function()
	toAdd = 0
	cra:FireServer(toAdd)
end)
Min.Activated:Connect(function()
	if VPFHolder:GetChildren()[1] ~= nil then
		local ItemVPF = VPFHolder:GetChildren()[1]

		local ItemNum = ItemVPF:WaitForChild("ItemNum").Value
		local MaxDur = ItemFolder[ItemNum].MaxDur.Value
		local CurrDur = ItemFolder[ItemNum].CurrDur.Value
		
		toAdd = 0
		cra:FireServer(toAdd)
	end
end)
Sub.Activated:Connect(function()
	if VPFHolder:GetChildren()[1] ~= nil then
		local ItemVPF = VPFHolder:GetChildren()[1]

		local ItemNum = ItemVPF:WaitForChild("ItemNum").Value
		local MaxDur = ItemFolder[ItemNum].MaxDur.Value
		local CurrDur = ItemFolder[ItemNum].CurrDur.Value
		
		if CurrDur + toAdd - 1 >= CurrDur then
			toAdd -= 1
			cra:FireServer(toAdd)
		end
	end
end)
Add.Activated:Connect(function()
	if VPFHolder:GetChildren()[1] ~= nil then
		local ItemVPF = VPFHolder:GetChildren()[1]

		local ItemNum = ItemVPF:WaitForChild("ItemNum").Value
		local MaxDur = ItemFolder[ItemNum].MaxDur.Value
		local CurrDur = ItemFolder[ItemNum].CurrDur.Value

		if CurrDur + toAdd + 1 <= MaxDur then
			toAdd += 1
			cra:FireServer(toAdd)
		end
	end
end)
Max.Activated:Connect(function()
	if VPFHolder:GetChildren()[1] ~= nil then
		local ItemVPF = VPFHolder:GetChildren()[1]

		local ItemNum = ItemVPF:WaitForChild("ItemNum").Value
		local MaxDur = ItemFolder[ItemNum].MaxDur.Value
		local CurrDur = ItemFolder[ItemNum].CurrDur.Value

		toAdd = MaxDur - CurrDur
		cra:FireServer(toAdd)
	end
end)

I think I have figured out the original problem. When ChildAdded activates, the functions inside of it will forever be able to activate and when multiple children are added, the function will be able to activate multilpe times. I may be wrong though.

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