Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Syntax error: Expected 'then' when parsing if statement, got '=', How do I fix this?

Asked by 3 years ago
Edited 3 years ago

I got this error while writing a script that deactivates Roblox studio videos when you touch a certain part and activates another video. (for a 2d game, couple of my stages are set in a city and I wanna make ads for billboards in the background.)

script.Parent.Touched:Connect(function()
    if workspace.Advertisement2.SurfaceGui.BloxyCola.Playing = false then
        workspace.Advertisement1.SurfaceGui.Eat.Playing = true
    elseif
        workspace.Advertisement2.SurfaceGui.BloxyCola.Playing = true then
        workspace.Advertisement1.SurfaceGui.Eat.Playing = false
    end
end)

I tried putting == instead of just 1 '=' but that just made it not work. I put prints in between each line and the script goes through, Just the video doesn't play. so I'm pretty sure its because of those double equals.

0
If statments require two =='s. WizyTheNinja 834 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

So, the reason you get that error is because the code right here:

if workspace.Advertisement2.SurfaceGui.BloxyCola.Playing = false then

as well as:

elseif
    workspace.Advertisement2.SurfaceGui.BloxyCola.Playing = true then

doesn't have a second "=". To fix this, you simply have to add another = right next to the currently existing one, as shown here:

if workspace.Advertisement2.SurfaceGui.BloxyCola.Playing == false then

The reason you must have two equals signs instead of one when working with if statements is that the equals sign has dual purposes.

One as a variable definer:

MyVariable = 50

And the other as a if statement:

if MyVariable == 50 then
    print("MyVariable has exceeded up to " .. MyVariable)
end

The difference between using it as a variable definer and a if statement operator is that: when defining variables, defining variables simply needs one equals sign, as it's defining a value to a variable, much like how 2+2 = 4. The other needs two equals signs because you are not defining a value to a variable, instead; you are comparing two values together, and if they are identical to each other, it results as true, otherwise its false.

In summary, use one equals sign for defining variables, and use two for comparing two values in if statements.

Ad

Answer this question