If statement not working

I made a opening/closing menu, but for some reason the if statement does not work. I tried making it a string, a BoolValue, and still no result.
Am i missing something here?
I will send the rbxm file as many things could be causing the problem.

1 Like

—Deleted it cuz idk—
here.

Could you send the code that doesn’t work instead?

Sure, but looking at the code doesnt really help that much

local value = true
local function closeAllMenus()
    TS:Create(PieceInfo, TweenInfo.new(0.3), {Position = UDim2.new(2.54, 0, 0.997, 0)})
    TS:Create(SelectionMenu, TweenInfo.new(0.3), {Position = UDim2.new(1.088, 0, 1.55, 0)}):Play()
    value = false
end

local function ToggleSelectionMenu()
    closeAllMenus()
    if value == true then
        print("closing")
        closeAllMenus()
    elseif value == false then
        print("opening")
        TS:Create(PieceInfo, TweenInfo.new(0.3), {Position = UDim2.new(2.54, 0, -0.017, 0)}):Play()
        TS:Create(SelectionMenu, TweenInfo.new(0.3), {Position = UDim2.new(1.088, 0, 0.499, 0)}):Play()
        value = true
    end
end

Hotbar.Arsenal.Activated:Connect(function()
    ToggleSelectionMenu()
end)

while wait() do
    print(value)
end

The while loop prints true when it’s true, but the ToggleSelectionMenu only runs the elseif value == false for some reason.
It will always print “opening”

Ok so I checked the script multiple times, what I see is that your function is overwriting the value.

Basically it sets it true, perfectly fine. However when you click it runs the function again, meaning the value will be set to false once more and so on.

I tried doing that in both. But it still gave me the same result - I see you edited your message

that is a fair point, but value is a variable that’s not local, and im not doing local value = false. Which means that im setting it for the whole script.
Event if i was wrong now, the while loop prints true when it’s true, and false when was false. So I don’t think that’s the case

Fixed version (basically just removed the closeAllMenus function)

local Main = script.Parent.Main
local Hotbar = Main:WaitForChild("Hotbar")
local SelectionMenu = Main:WaitForChild("SelectionMenu")
local PieceInfo = Main:WaitForChild("PieceInfo")

local TS = game:GetService("TweenService")

local value = true
local function closeAllMenus()
    TS:Create(PieceInfo, TweenInfo.new(0.3), {Position = UDim2.new(2.54, 0, 0.997, 0)})
    TS:Create(SelectionMenu, TweenInfo.new(0.3), {Position = UDim2.new(1.088, 0, 1.55, 0)}):Play()
    value = false
end

local function ToggleSelectionMenu()
    if value == true then
        print("closing")
        closeAllMenus()
    elseif value == false then
        print("opening")
        TS:Create(PieceInfo, TweenInfo.new(0.3), {Position = UDim2.new(2.54, 0, -0.017, 0)}):Play()
        TS:Create(SelectionMenu, TweenInfo.new(0.3), {Position = UDim2.new(1.088, 0, 0.499, 0)}):Play()
        value = true
    end
end

Hotbar.Arsenal.Activated:Connect(function()
    ToggleSelectionMenu()
end)
1 Like

Oh I see. I think I missunderstood. I put closeAllMenus() before the function ran.
Thanks!

1 Like