MouseButton1Click troubles... Doubling values

I’m trying to create a Color Changer to my House System using UI’s replicated on client, but…
image
I don’t know why, the values ​​are coming out duplicated.
using print I can see what the script keeps doubling the values

it’s not the first time I’ve seen myself with this kind of problem, but this time the “Disconnect()” option doesn’t work

The Client Script:

local MouseEvent
local ServerTG

function SetPaint()
	
	for i, child in pairs(PaintBG.ScrollingFrame:GetChildren()) do
		if child:IsA("TextButton") then child:Destroy()	end end
	
	for i, color in pairs(game.ReplicatedStorage:WaitForChild("HouseSystem").Colors:GetChildren()) do
		local ColorSelectClone = script.ColorSelect:Clone() 
		ColorSelectClone.Parent = PaintBG.ScrollingFrame
		ColorSelectClone.BackgroundColor3 = color.Value.Color
		
		MouseEvent = ColorSelectClone.MouseButton1Click:Connect(function()
			MouseEvent:Disconnect()
			PaintBG.ColorPreview.BackgroundColor3 = color.Value.Color PaintBG.ColorName.Text = tostring(color.Value.Name)			
			
			for i, colorpart in pairs(game.Players.LocalPlayer.PlayerHouseInfoFolder.ActualHouse.Value.Main:GetChildren()) do
				colorpart.BrickColor = color.Value
			end
		end)	
	end
	
	ServerTG = PaintBG.Confirm.MouseButton1Click:Connect(function()
		ServerTG:Disconnect()
			print(PaintBG.ColorName.Text)
	end)
end

while wait(1) do
	--UpdateInventorySelect()
	SetPaint()
end

Could someone please point out what’s wrong with my script?

Its because you call setpaint in a while loop that iterates ever second. And in that function you make connections to an event. So each second a new connection is being made meaning the same function will run multiple times on one event fire

Fix would be you disconnect both events in first lines of SetPaint() where right now you disconnect them when the connected event fires

I always thought the best solution to keep a ui up to date was to use “while” to always update every second.
But still calling SetPaint() just once, the colors don’t show up in the UI.

Disconnect the events on the line before you connect them. Still might not work but try it out see what changes

1 Like

Wait a minute i just realized the other mistake you made. You are connecting different events using the same variable. So when MouseClick is assigned to a new connection, you wont have access to the connection you replaced. Its never disconnected

To fix just do local MoveClick = connection. That way you create a new variable each time which will be the one being referenced in the function

can you tell me where it is, i don’t see

Yeah ill edit the code rn just give me a sec

I just found another solution
I remove setpaint(), now the lines are free in the script and it seems that it worked, I’m using as a reference to change the color of the player’s house in the client, the name of the color in question which is in: “PaintBG.ColorName.Text”

1 Like
ServerTG = PaintBG.Confirm.MouseButton1Click:Connect(function()
		--ServerTG:Disconnect()
		print(PaintBG.ColorName.Text)
	end)
1 Like

local ServerTG

function SetPaint()

    for i, child in pairs(PaintBG.ScrollingFrame:GetChildren()) do
		if child:IsA("TextButton") then child:Destroy()	end end
	
	for i, color in pairs(game.ReplicatedStorage:WaitForChild("HouseSystem").Colors:GetChildren()) do
		local ColorSelectClone = script.ColorSelect:Clone() 
		ColorSelectClone.Parent = PaintBG.ScrollingFrame
		ColorSelectClone.BackgroundColor3 = color.Value.Color
		
		local MouseEvent = ColorSelectClone.MouseButton1Click:Connect(function()
			MouseEvent:Disconnect()
			PaintBG.ColorPreview.BackgroundColor3 = color.Value.Color PaintBG.ColorName.Text = tostring(color.Value.Name)			
			
			for i, colorpart in pairs(game.Players.LocalPlayer.PlayerHouseInfoFolder.ActualHouse.Value.Main:GetChildren()) do
				colorpart.BrickColor = color.Value
			end
		end)	
	end
	
    ServerTG:Disconnect()
	ServerTG = PaintBG.Confirm.MouseButton1Click:Connect(function()
		ServerTG:Disconnect()
			print(PaintBG.ColorName.Text)
	end)
end

Mk finished. Didnt mean to delete the while loop so keep that in there. If you need to use it like that then go for it. Only thing is you gotta be careful when connecting events. Cuz if not checked you could have thousands of connections causing alot of lag

1 Like

It seems to work fine, with some modifications. Thanks friend, you just saved me!

1 Like


look, working perfectly, ty again!

1 Like