Event firing multiple time and multiple character cloning

  1. What do you want to achieve? I want to fix 2 problems in my script.

  2. What is the issue? When i click on a button it fires an event multiple times and it clones a different character in the viewport frame.

  3. What solutions have you tried so far? Debugging, Devforum, remaking the script and chatgpt.

So basically my script is a handler script, that automatically makes frames depending on the items that are in a folder in replicatedStorage and it makes the function. The problem is: When clicking on a button the event fires multiple times and there are multiple characters cloning in the viewport frame.

		local template = script.Template:Clone()
		template.Parent = charactersFrame.ScrollingFrame

		local name = folder.Name
		local equipped = folder.Equipped
		local rarity = folder.Rarity
		
		local camera = Instance.new("Camera", template.ViewportFrame)
		template.ViewportFrame.CurrentCamera = camera
		
		local characterModel = game.ReplicatedStorage.Characters[name]:Clone()
		characterModel.Parent = template.ViewportFrame

		local hrp = characterModel.PrimaryPart
		camera.CFrame = CFrame.new(hrp.Position + hrp.CFrame.LookVector * 7, hrp.Position)

		template.ViewportFrame.IconName.Text = name

		if raritiesColors[rarity.Value] then
			template.ViewportFrame.IconName.TextColor3 = raritiesColors[rarity.Value]
		end

		if equipped.Value == true then
			template.MainButton.Text = "Equipped"
		else
			template.MainButton.Text = "Equip"
		end

		template.MainButton.MouseButton1Click:Connect(function()
			if template.MainButton.Text == "Equip" then
				local success = charEvents.Action:InvokeServer("Equip", name)

				if success == "Successful Equip" then
					for _, frame in pairs(charactersFrame.ScrollingFrame:GetChildren()) do
						if frame:IsA("Frame") then
							frame.MainButton.Text = "Equip"
						end
					end
					wearSound:Play()
					template.MainButton.Text = "Equipped"
				else
					local errorTemplate = script.ErrorTemplate:Clone()
					errorTemplate.ErrorMessage.Text = success

					if ui:FindFirstChild("Error") then
						ui:FindFirstChild("Error"):Destroy()
					end

					errorTemplate.Name = "Error"
					errorTemplate.Parent = ui
					wait(2)
					errorTemplate:Destroy()
				end
			else
				clickSound:Play()
			end
		end)

		template.Name = name

		if template.Name == "Noobie" then
			template.LayoutOrder = 0
		else
			template.LayoutOrder = raritiesOrder[rarity.Value]
		end
	end

I’m not askin a full script, i just need an explanation and a possible way to fix my problem. Thanks for responding!

You simply need to add a debounce variable set to false and whenever you press the button it will check if debounce = false and then it will put it to true wait and put it back to false, i hope you understand

that’s what i did for the server script: But do you have an idea how to stop the multiple character cloning?

Just do the same for the script that clones it? Maybe you could check whether a character is already in the ViewportFrame, and then delete it before adding another character so that it doesn’t look glitchy.

If this doesnt work, you could delete everything inside the frame and then add the clone

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