1
votes

Problem

So currently, I am making a game in Roblox. I am working on tweening one of my GUIs, but the code isn't changing the var I am working with called state. The state var is supposed to tell if it's open or closed (State would = true if open, else, State would = false).

I have tried to make the variable a local var. But still the same output. I checked the output by printing the state var. Which would always be the same as the default value.

Code


-- Local Variables
local Frame = script.Parent.Parent.Parent.Parent.Parent.MinerGuiManager.MinerFrame
State = false
local Button = script.Parent.Button


-- Open/Close Statements

if State == true then
    Button.Text = 'Close!'
    script.Parent.Button.MouseButton1Click:connect(function()
    Frame:TweenPosition(UDim2.new(0.3,0,1.2,0))
    State = false
end)
end

if State == false then
    Button.Text = 'Open!'
    script.Parent.Button.MouseButton1Click:connect(function()
    Frame:TweenPosition(UDim2.new(0.305,0,0.25,0,'Bounce',1.5))
    State = true
end)
end

I expect the output of the code to set the var state to be True when open and False when closed.

2
Lua variables are case-sensitive, in your edit, you put in state instead of State - This does not resolve your issue though.dualed

2 Answers

2
votes

You need to be careful about how you connect your Mouse1Click event listeners.

When you read your script from top to bottom, you'll see that since State starts as false, the only listener you've connected is the second one. This means that when you click on the button, it will only ever tween the Frame into the Open state. It is better to write one click handler that handles this logic on every click.

local Button = script.Parent.Button
local Frame = script.Parent.Parent.Parent.Parent.Parent.MinerGuiManager.MinerFrame
State = false

Button.MouseButton1Click:connect(function()
    if State == true then
        Button.Text = 'Close!'
        Frame:TweenPosition(UDim2.new(0.3,0,1.2,0))
    else
        Button.Text = 'Open!'
        Frame:TweenPosition(UDim2.new(0.305,0,0.25,0,'Bounce',1.5))
    end)

   State = not State
end)
1
votes

You are missing State = false after Frame:TweenPosition(UDim2.new(0.3,0,1.2,0)) line. You never switch its value back to false after it was changed to true.