How Do I Tween Part through a GUI Button?

Hey there!

So recently I’ve been trying to make it so when I click a GUI button, a part tweens and a part changes color. So far I’ve tried this (and MouseButton1Click) but so far nothing has worked and there’s no errors. If anybody can help me that would be amazing! Thank you!

-- local door = script.Parent.Door
local btn = game.StarterGui.ComputerGUI.Frame.ImageLabel["Turn On"]
local doorCFrame = door.CFrame
local doorendCFrame = script.Parent.DoorEnd.CFrame

local TweenService = game:GetService("TweenService")

local TweenInformation = TweenInfo.new(
	0.50,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)

local TweenA = TweenService:Create(door, TweenInformation, {CFrame = doorendCFrame})
local Tweenb = TweenService:Create(door, TweenInformation, {CFrame = doorCFrame})

local DoorOpen = false
	btn.MouseButton1Up:Connect(function()
	script.Parent.Industrial_Light.Light.BrickColor = BrickColor.new("Bright green")
	script.Parent.Industrial_Light.Light2.BrickColor = BrickColor.new("Medium green")
	script.Parent.Industrial_Light.Light2.PointLight.Enabled = true
	if DoorOpen == false then
		DoorOpen = true
		TweenA:Play()
script.Parent.Part:Destroy()
	end
	end)

Multiple things wrong with this script.

  1. LocalScripts don’t run in workspace
  2. StarterGui references what the player gui starts as, not what it currently is

What I would do is put the LocalScript into your “Turn On” GUI, and reference all variables there.

Code:

--//Services
local TweenService = game:GetService("TweenService")

--//Variables
local Button = script.Parent
local Door = workspace.Door --//Change if needed

--//Controls
local DoorOpen = false
local startCFrame = Door.CFrame
local endCFrame = Door.DoorEnd.CFrame

local TweenInformation = TweenInfo.new(
	0.50,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.InOut,
	0,
	false,
	0
)

local TweenA = TweenService:Create(Door, TweenInformation, {CFrame = endCFrame})
local Tweenb = TweenService:Create(Door, TweenInformation, {CFrame = startCFrame})

--//Functions
Button.MouseButton1Down:Connect(function()
	Door.Industrial_Light.Light.BrickColor = BrickColor.new("Bright green")
	Door.Industrial_Light.Light2.BrickColor = BrickColor.new("Medium green")
	Door.Industrial_Light.Light2.PointLight.Enabled = true
		
	if not DoorOpen then
		DoorOpen = true
		TweenA:Play()
		Door.Part:Destroy()
	end
end)
1 Like

why not just use remote events for this?

Thank you! This worked for me. Really appreciate the help! :smiley:

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