One button connection work not the other one

Basically for a plugin i’m making a selection gui for when a script might be harmfull for the game the player can allow it or delete it to prevent issues with his game.

However i found myself in an issue with the plugin where only the Allow button is working.

What i attempted:
Add separate running function with task.spawn().

Code:

-- Display an alert for the user to select if keeping or removing or leaving it
	local function DisplayAlert(object,reason)

		ProtectionGUI.Alert.Visible = true
		ProtectionGUI.Alert.Reason.Text = object.Name.." had been quarantined for the following reason: "..reason

        -- Allow button
		task.spawn(function()
			connection1 = ProtectionGUI.Alert.Allow.MouseButton1Click:Connect(function()
				if object:FindFirstChild("OriginalParent") then
					if connection1 then
						connection1:Disconnect()
					end

					if connection2 then
						connection2:Disconnect()
					end

					object.Parent = object.OriginalParent.Value
					object.Enabled = true
					object:SetAttribute("AllowedScript",true)
					object.OriginalParent:Destroy()
					ProtectionGUI.Alert.Visible = false
				else
					print("Cant allow the suspected script at the time")
				end
			end)
		end)

        -- Delete Button
		task.spawn(function()
			connection2 = ProtectionGUI.Alert.Destroy.MouseButton1Click:Connect(function()
				if connection1 then
					connection1:Disconnect()
				end

				if connection2 then
					connection2:Disconnect()
				end

				object:Destroy()
				ProtectionGUI.Alert.Visible = false
			end)
		end)

		task.wait(7)
		ProtectionGUI.Alert.Visible = false
		if connection1 then
			connection1:Disconnect()
		end

		if connection2 then
			connection2:Disconnect()
		end
	end

	-- Display an alert to the user
	if not ProtectionGUI.Alert.Visible then
		DisplayAlert(object,reason)
	end

	-- If an alert is alredy poped up then wait
	if ProtectionGUI.Alert.Visible then
		repeat wait() until not ProtectionGUI.Alert.Visible

		DisplayAlert(object,reason)
	end
2 Likes


This is for the output console

It’s probably because your instance is named Destroy which is shadowed by the name of the instance.Destroy method.

Try renaming your gui to something other than “Destroy”, or use :FindFirstChild(“Destroy”)

As an aside, I want to point out another issue; each time you display your alert, you are recreatingi the button connections. To avoid this, you should be disconnecting both connections once either of them are clicked (or set it up in a way where the connections are set up only once, but that gets tricky for concurrent alerts).

And you don’t need task.spawn around connections, they will start their own threads when the event happens.

Also if you end up calling DisplayAlert while another alert is already displaying, this will cause a problem where the previous alert’s code will hide the new alert. Consider adding a debounce that depends on whether it’s already shown, or implement a queue so it queues up the alert until the previous one is closed.

i planed to remove the task.spawn it was just a way to see if the script gets stuck by the first event.