简体   繁体   中英

If statement not working in roblox studio

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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM