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

expected identifier when parsing expression, got 'then", how do i fix this?

Asked by 1 year ago

local maingui = script.Parent.Parent.main local bvalue = script.active local pos1 = "{0.014, 0},{0.03, 0}" local pos2 = "{15, 15},{15, 15}" script.Parent.MouseButton1Click:Connect(function() if bvalue.Value = true then maingui.Position = UDim2.fromOffset("pos1") bvalue.Value = false else maingui.Position = UDim2.fromOffset("pos2") bvalue.Value = true end end)

im not really a scripter and im just trying to make this work, idk what to do lol ingame its expected 'then' when parsing if statement, got '='

2 answers

Log in to vote
1
Answered by
Benbebop 1049 Moderation Voter
1 year ago

Main Problem

Your first issue is you are using =, used to assign a variable, when you should be using ==, used to compare two values.

local var1 = 5
local var2 = 6

print(var1 == var2)

false

Just for clarification, print outputs anything within it to the console.

Other issues

First of all, when you index a variable you are putting it in quotes. Quotes (as well as apostrophes and double square brackets) are used to construct a string. So if you try to index a variable inside of it you will just have a string of the variable name. You will want to get rid of the quotes.

local var1 = "this is text in var1"
local var2 = "this is text in var2"

print("var1")
print(var2)

var1
this is text in var2


You seem to have a misunderstanding of how UDim2 works. A UDim2 is composed of 4 numerical values.

{scale x, offset x},{scale y, offset y}

scale and offset are a very important distinction. Scale is a ratio of how far along its parent instance you want the position value (0 through 1) whereas offset is just how many pixels along its parent instance you want the position (any integer).

For example the UDim2 {0.5, 15},{0.5,15} would represent a point in the center of the screen moved by 15 pixels in the x and y.

fromOffset will create a UDim2 with the form {0, x},{0, y}, you ether want to use fromScale or new.


Lastly UDim2 requires numbers, not a string.

UDim2.new("{0.5, 15},{0.5,15}") -- will error (something about expecting a number)

UDim2.new(0.5, 15, 0.5,15) -- will work as intended

That should be it

if you have any more questions im happy to answer

Ad
Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
1 year ago

There were multiple things wrong with the formatting with your code that you supplied

Step through explanation:

Your code:

local pos1 = "{0.014, 0},{0.03, 0}" 
local pos2 = "{15, 15},{15, 15}"

Assuming these are meant to be UDim2 values for a GUI element position?. The way you've formatted these are read by Luau (or simply put Roblox Lua) would be interpreted as a string and not an UDim value. The correct way to format UDim Values is like this:

-- AKA .........UDim2.new(UDim,UDim) -- a UDim is simply the offset and scale for your gui element to use!. (x Scale,x Offset), (y Scale,y Offset) 

local pos1 = UDim2.new(0.014, 0, 0.03, 0) 
local pos2 = UDim2.new(15, 15, 15, 15)

Moving on to the next problem we come to this line:

if bvalue.Value = true then 

in Lua this would be setting the bvalue.Value to equal true rather than checking it if is true or not!. To fix this simply do this:

if bvalue.Value == true then 

Also the error code is happening on this line. The Lua interpreter is expecting you setting your bvalue.Value to equal true, but it finds the then at the end of the line so it simply spits out the problem and thats the error you got!.

The last problems is with these lines:

       maingui.Position = UDim2.fromOffset("pos1") 
       bvalue.Value = false 
else 
       maingui.Position = UDim2.fromOffset("pos2") 
       bvalue.Value = true end end)

This will also error out due to you passing a string and not your position values you set above. the function UDim2.fromOffset() expects a Vector2 and not a string value. Here you're simply feeding it with a string!.

To fix these i would change it to :

    maingui.Position = pos1 -- just pass the stored value above to this no need for the UDim2.fromOffset() function for this!.
    bvalue.Value = false 
else 
    maingui.Position = pos2 -- just pass the stored value above to this no need for the UDim2.fromOffset() function for this!.
    bvalue.Value = true 

Full Code:

local maingui = script.Parent.Parent.main 
local bvalue = script.active 
local pos1 = UDim2.new(0.014, 0, 0.03, 0) 
local pos2 = UDim2.new(15, 15, 15, 15)
script.Parent.MouseButton1Click:Connect(function() 
    if bvalue.Value == true then 
        maingui.Position = pos1 
        bvalue.Value = false 
    else 
        maingui.Position = pos2
        bvalue.Value = true 
    end 
end)

Hope this helps! :) if you have any problems or questions, let us know! :)

Answer this question