Code is working weirdly

I have made a spell equip thingie, but each time i do it it dupes it even more

here is the code

local isSelecting = script.Parent.Parent.isSelecting
local player = game.Players.LocalPlayer

local button = script.Parent

button.MouseButton1Click:Connect(function()
	if isSelecting.Value == false then
		isSelecting.Value = true
		button.BackgroundColor3 = Color3.new(1, 1, 1)
		
		
		
		
		player.PlayerGui.Main.PlaceHere.TextTransparency = 0
		player.PlayerGui.Main.PlaceHere.UIStroke.Transparency = 0
		player.PlayerGui.Main.PlaceHere.Visible = true



		local buttonTemplate = player.PlayerGui.Main.Buttons.bTemplate

		player.PlayerGui.Main.PlaceHere.MouseButton1Click:Connect(function()
			isSelecting.Value = false
			local clonedButton = buttonTemplate:Clone()

			clonedButton.Text = button.Text
			clonedButton.Visible = true
			clonedButton.Parent = player.PlayerGui.Main.Buttons
			

			
			button.Visible = false
			local Info = TweenInfo.new(1)
			local Tween = game:GetService("TweenService"):Create(player.PlayerGui.Main.PlaceHere,Info,{TextTransparency=1})
			Tween:Play()
			local Infor = TweenInfo.new(1)
			local Tweena = game:GetService("TweenService"):Create(player.PlayerGui.Main.PlaceHere.UIStroke,Infor,{Transparency=1})

			Tweena:Play()
			
			wait(1)
	
			player.PlayerGui.Main.PlaceHere.Visible = false




		end)
	end
end)
1 Like

Does it work fine the first time, but then subsequent tries starts adding more and more?

You could try changing

player.PlayerGui.Main.PlaceHere.MouseButton1Click:Connect(...

to

player.PlayerGui.Main.PlaceHere.MouseButton1Click:Once(...

Since you’re connecting the MouseButton1Click and not disconnecting it, every time the label is pressed it generates a new connection, so eventually you have all of these open connections listening to the same things, and doing the same function multiple times. Using the Once code makes it so you dont have to worry about disconnecting after it runs one time which should keep the issue from happening

1 Like

Hmm its still the same effect, but thanks for your help

How would i disconnect it like you said

When you Connect an event (so it starts calling your function), you can Disconnect it to make it stop calling the function. Example:

local button -- this is some button you've defined

local connection = button.MouseButton1Click:Connect(function()
    print("I was clicked")
end)

-- Notice how I have a variable called connection
-- It has a RBXScriptConnection value inside of it (returned by :Connect)

task.wait(10) -- wait a bit
connection:Disconnect()

-- After the 10 seconds, the function will no longer get called
-- and so nothing will be printed

You should try adding a debounce whenever you click the “PlaceHere” check if the debounce is false and then set the debounce to true after 1-3 seconds set it to false :man_shrugging:

I don’t know if you solved this, but here’s the soultion i’ve figured out for you:

local isSelecting = script.Parent.Parent.isSelecting
local player = game.Players.LocalPlayer

local button = script.Parent

button.MouseButton1Click:Connect(function()
	if isSelecting.Value == false then
		isSelecting.Value = true
		button.BackgroundColor3 = Color3.new(1, 1, 1)
		
		
		
		
		player.PlayerGui.Main.PlaceHere.TextTransparency = 0
		player.PlayerGui.Main.PlaceHere.UIStroke.Transparency = 0
		player.PlayerGui.Main.PlaceHere.Visible = true



		local buttonTemplate = player.PlayerGui.Main.Buttons.bTemplate
		local connection --Blank value to attach connection to it.
		
		connection = player.PlayerGui.Main.PlaceHere.MouseButton1Click:Connect(function()
			connection:Disconnect() --Disconnecting the function after click.
			isSelecting.Value = false
			local clonedButton = buttonTemplate:Clone()

			clonedButton.Text = button.Text
			clonedButton.Visible = true
			clonedButton.Parent = player.PlayerGui.Main.Buttons
			

			
			button.Visible = false
			local Info = TweenInfo.new(1)
			local Tween = game:GetService("TweenService"):Create(player.PlayerGui.Main.PlaceHere,Info,{TextTransparency=1})
			Tween:Play()
			local Infor = TweenInfo.new(1)
			local Tweena = game:GetService("TweenService"):Create(player.PlayerGui.Main.PlaceHere.UIStroke,Infor,{Transparency=1})

			Tweena:Play()
			
			wait(1)
	
			player.PlayerGui.Main.PlaceHere.Visible = false




		end)
	end
end)
1 Like

Thanks you! it works perfectly fine now!

1 Like

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