Home > Software engineering >  If statement not working in roblox studio
If statement not working in roblox studio

Time:06-22

local Frame = script.Parent.Parent.Frame.BackgroundTransparency
local Red = Frame.RedTeam.BackroundTransparency
local Blue = Frame.BlueTeam.BackroundTransparency
local Button = script.Parent

Button.MouseButton1Click:Connect(
    if Frame = 1 then
        Frame = 0
        Red = 0
        Blue = 0
    end
    if Frame = 0 then
        Frame = 1
        Red = 1
        Blue = 1
    )

At

if Frame = 1 then
        Frame = 0
        Red = 0
        Blue = 0
    end

I get "Expected else when parsing, instead got =". Also, at the "end" I get "Expected eof, instead got end"

I cant figure this out and nothing is working.

CodePudding user response:

Replace if Frame = 1 then with if Frame == 1 then

If you want to check wether two values are equal you need to use the equality operator ==, not the assignment operator =.

Also your second if statement is missing its closing end.

Further you probably want to think about your logic.

If Frame equals 0 you'll assign 1. But then you'll end up in the next if statement with the condition Frame == 1 and assign 0 again.

So your code basically does nothing useful.

CodePudding user response:

In lua, if statements use operators which are ==, ~=, >, <, >=, and <=. the '==' statement basically compares if the first argument they get is the same as the second argument they get, which is the case for you here.

So you'll need to change the Frame = 1 and the Frame = 0 to Frame == 1 and the Frame == 0.

and at the end of your function, it is missing an end there. And also at the start it is not calling a new function. And for peak optimization, instead of using 2 if statements, you can just use 1 with an elseif. Correction:

local Frame = script.Parent.Parent.Frame.BackgroundTransparency
local Red = Frame.RedTeam.BackroundTransparency
local Blue = Frame.BlueTeam.BackroundTransparency
local Button = script.Parent

Button.MouseButton1Click:Connect(function()
    if Frame == 1 then
        Frame = 0
        Red = 0
        Blue = 0
    elseif Frame == 0 then
        Frame = 1
        Red = 1
        Blue = 1
    end
end)
  • Related