Script button not changing or making function work

local btn = script.Parent.Inventory
local frame = script.Parent.Frame
local btn2 = script.Parent.Inventory2
local z = true

btn.MouseButton1Click:Connect(function()
	if z == true then
		frame:TweenPosition(UDim2.new(0.13, 0,1.5, 0),"Out",1)
		z = false
	end
	if z == false then
		frame:TweenPosition(UDim2.new(0.13, 0,0.115, 0),"Out",1)
		z = true
	end
end)

im trying to make a tweenservice to make a button go up and down, but it isnt working at all

local script
its in the startergui

Actually it works but it takes like 4-5 clicks for it to work

The “Tween:Play()” Is in red.
Doesn’t work

What’s actually happening:

local btn = script.Parent.Inventory
local frame = script.Parent.Frame
local btn2 = script.Parent.Inventory2
local z = true

btn.MouseButton1Click:Connect(function()
	if z == true then -- z == true, passes the check
		frame:TweenPosition(UDim2.new(0.13, 0,1.5, 0),"Out",1)
		z = false -- Changes "z" to false
	end
	if z == false then -- z == false, passes the check
		frame:TweenPosition(UDim2.new(0.13, 0,0.115, 0),"Out",1)
		z = true -- Changes "z" back to true
	end
end)
  • Line 7: z checks if it’s equal to true (Passes)
  • Line 9: z changes to false
  • Line 11: z now checks if it’s equal to false (Passes)
  • Line 13: z changes back to true

z is never switching back to false the way you’d expect it to be

Also make sure that you’re referencing the correct UIObjects within your variables, along with TweenPosition’s Parameters

local btn = script.Parent.Inventory
local frame = script.Parent.Frame
local btn2 = script.Parent.Inventory2
local z = true

btn.MouseButton1Click:Connect(function()
    z = not z
	
	if z == true then
		frame:TweenPosition(UDim2.new(0.13, 0,1.5, 0), "Out", "Quad", 1, true)
	end
	
	if z == false then
		frame:TweenPosition(UDim2.new(0.13, 0,0.115, 0), "Out", "Quad", 1, true)
	end
end)
1 Like

Works perfectly! Thanks! I will use it for future scripts

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