Issues with CanvasGroup

I am aiming to make my menu disappear when a button inside of it is clicked, and I would like to use a CanvasGroup for this.
image

My click script does not seem to work.

local TweenService = game:GetService("TweenService")
local gui = script.Parent
local canvasGroup = gui:FindFirstChild("CanvasGroup")

-- Set the initial alpha value to 1 (fully visible)
canvasGroup.Alpha = 1

-- Define the tween info for the animation
local tweenInfo = TweenInfo.new(
	0.5, -- Duration of the tween
	Enum.EasingStyle.Quad, -- Easing style of the tween
	Enum.EasingDirection.Out -- Easing direction of the tween
)

-- Define the goal properties for the tween
local goal = {
	Alpha = 0 -- Target alpha value (fully transparent)
}

-- Create the tween
local tween = TweenService:Create(canvasGroup, tweenInfo, goal)

-- Connect the MouseButton1Click event
gui.Activated:Connect(function()
	-- Play the tween to make the instances inside the CanvasGroup invisible
	tween:Play()
end)

I have tried to use an in pairs loop but it turned out to be quite messy, and I wanted to try the canvas group as it is apparently easier and can manipulate all it’s children.
Anyone got an idea how I can fix this?
Thanks!

Alpha is not a real property, use GroupTransparency = 1 instead and see

change this line to canvasGroup.GroupTransparency = 0

2 Likes

The script you’re using here seems to be made using chatGPT, please don’t use that to make scripts because it sucks at it
here is an updated script that should work

local TweenService = game:GetService("TweenService")
local gui = script.Parent
local canvasGroup = gui:FindFirstChild("CanvasGroup")

-- Set the initial alpha value to 1 (fully visible)
canvasGroup.GroupTransparency = 1

-- Define the tween info for the animation
local tweenInfo = TweenInfo.new(
	0.5, -- Duration of the tween
	Enum.EasingStyle.Quad, -- Easing style of the tween
	Enum.EasingDirection.Out -- Easing direction of the tween
)

-- Define the goal properties for the tween
local goal = {
	GroupTransparency = 0 -- Target alpha value (fully transparent)
}

-- Create the tween
local tween = TweenService:Create(canvasGroup, tweenInfo, goal)

-- Connect the MouseButton1Click event
gui.MouseButton1Click:Connect(function()
	-- Play the tween to make the instances inside the CanvasGroup invisible
	tween:Play()
end)

i’ve changed Alpha to GroupTransparency as alpha doesn’t exist,
and to check if the button is clicked you use MouseButton1Click

3 Likes

Thanks for the response, however it still seems to not work.

My bad, I had the wrong localisation for the canvasGui.
Here is my final script:

local TweenService = game:GetService("TweenService")
local gui = script.Parent
local canvasGroup = gui.Parent.Parent

canvasGroup.GroupTransparency = 0

local tweenInfo = TweenInfo.new(
	0.5, 
	Enum.EasingStyle.Quad,
	Enum.EasingDirection.Out 
)


local goal = {
	GroupTransparency = 1
}


local tween = TweenService:Create(canvasGroup, tweenInfo, goal)


gui.MouseButton1Click:Connect(function()
	tween:Play()
end)
1 Like

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