Roblox Error: Expected ')' to close '(' at column 3), got '=' - lua

Hi I am a user on Roblox and I am trying to script a light switch that turns off 4 lights and I am having a error (it's in the title)
There are 2 blocks being used, the Off4 and On4 switch.
My code is
function OnClicked()
if (workspace.LivingRoomLight.SpotLight.Enabled == true) and (workspace.LivingRoomLight2.SpotLight.Enabled == true) and (workspace.LivingRoomLight3.SpotLight.Enabled == true) and (workspace.LivingRoomLight4.SpotLight.Enabled == true) then
(workspace.LivingRoomLight.SpotLight.Enabled = false) and (workspace.LivingRoomLight2.SpotLight.Enabled == false) and (workspace.LivingRoomLight3.SpotLight.Enabled == false) and (workspace.LivingRoomLight3.SpotLight.Enabled == false)
script.Parent.Transparency = 1
workspace.Off4.Transparency = 0
end
end
script.Parent.ClickDetector.MouseClick:connect(OnClicked)
The other scripts (that work) I use in the ones that use only one light is
function OnClicked()
if (workspace.Hallwaylight.SpotLight.Enabled == true) then
workspace.Hallwaylight.SpotLight.Enabled = false
script.Parent.Transparency = 1
workspace.Off.Transparency = 0
end
end
script.Parent.ClickDetector.MouseClick:connect(OnClicked)
Note: I am only using the on scripts because that's the only one I edited for the one with the error. The error in the on script is the first = at column 3 and when I use '==' instead of '=' then the whole line becomes a error

Try this:
if (workspace.LivingRoomLight.SpotLight.Enabled == true) and (workspace.LivingRoomLight2.SpotLight.Enabled == true) and (workspace.LivingRoomLight3.SpotLight.Enabled == true) and (workspace.LivingRoomLight4.SpotLight.Enabled == true) then
workspace.LivingRoomLight.SpotLight.Enabled = false
workspace.LivingRoomLight2.SpotLight.Enabled = false
workspace.LivingRoomLight3.SpotLight.Enabled = false
workspace.LivingRoomLight4.SpotLight.Enabled = false
...
Some pointers:
x == y means “does x equal y?”. It’s a condition (either true or false).
x = y means “set x to y”. It’s a statement (a command to your program to modify the value of x).
and is an operator that expects conditions to its left and right.
Your program is of the form
if (these four values are true) then
set each of them to false
end
so you need and and == on the first line, but they don’t make sense inside the if — you need four simple statements using =, there.
You don’t really need == though. Comparing boolean values (like workspace.LivingRoomLight.SpotLight.Enabled, which are already true or false) to true is a bit silly: instead of if x == true then ... end it’s nicer to just write if x then ... end.

Related

how do i fix ')' (to close '(' at line 38), got '.'

for i, v in pairs(Buttons:GetChildren()) do
local NewItem = BoughtItems:FindFirstChild(v.Item.value)
if NewItem ~= nil then
Items[NewItem.Name] = NewItem:Clone()
NewItem:Destroy()
else
v.ButtonPart.Transparency = 1
v.ButtonPart.CanCollide = false
v.ButtonPart.BillBoardGui.Frame.Visible = false
end
if v:FindFirstChild("Dependency") then
coroutine.resume(coroutine.create(function(
v.ButtonPart.Transparency = 1
v.ButtonPart.CanCollide = false
v.ButtonPart.BillBoardGui.Frame.Visible = false
if BoughtItems:WaitForChild(v.Dependency.Value, 100000)then
v.ButtonPart.Transparency = 0
v.ButtonPart.CanCollide = true
v.ButtonPart.BillBoardGui.Frame.Visible = true
end
end))
end
v.ButtonPart.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid")then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Values.OwnerValue.Value == Player then
if v.ButtonPart.CanCollide == true and v.ButtonPart.Transparency == 0 then
if Player:WaitForChild("leaderstats").Cash.Value >= v.Price.Value then
Player.leaderstats.Cash.Value -= v.Price.Value
Items[v.Item.Value].Parent = BoughtItems
v:Destroy()
end
end
end
end
end)
end
how do i fix this the header is the problem i dont knw what it means so is there a fix to it for my tycoon if anyone knows pls comment (heres some random stuff since my post is mostly code) A brief Introduction to Copypasta. Copypasta means to copy a text or a part of the text from an existing manuscript and inclusion of that very text in an under-process manuscript by pasting. At present, the societies are going to be expanded with an enormous pattern.
Parenthesis always come in pairs. The error message already strongly suggests that you are missing a closing parenthesis for an opening one in line 38.
1 2 3
coroutine.resume(coroutine.create(function(
v.ButtonPart.Transparency = 1
v.ButtonPart.CanCollide = false
v.ButtonPart.BillBoardGui.Frame.Visible = false
if BoughtItems:WaitForChild(v.Dependency.Value, 100000)then
v.ButtonPart.Transparency = 0
v.ButtonPart.CanCollide = true
v.ButtonPart.BillBoardGui.Frame.Visible = true
end
end)) <--- one is missing
1 2
Use a text editor that autocompletes pairs or even better one that hightlights pairs in matching colors to avoid errors like this.

Why is this string not splitting in lua

So I am working on a project and I need to split a string that would look something like this:
if (x == 2){ output("Hello") }
This is my code:
local function splitIfStatement(str)
local t = {}
t[1] = ""
t[2] = ""
t[3] = ""
local firstSplit = false
local secondSplit = false
local thirdSplit = false
str:gsub(".", function(c)
if c == "(" then
firstSplit = true
end
if firstSplit == true then
if c == "=" then
firstSplit = false
secondSplit = true
else
if c == "(" then
else
t[1] = t[1] .. c
end
end
end
if secondSplit == true then
if c == ")" then
secondSplit = false
thirdSplit = true
else
if c == "=" then
else
t[2] = t[2] .. c
end
end
end
end)
return t
end
I need to split the string at "(" so t[1] is only equal to "x" and t[2] is equal to 2 and then t[3] is equal to the "output()"
But when I run my code(note I haven't added the t[3]) t[1] returns: "x "Hello") }" and t[2] returns 2 like it should.
Anyways why isn't the split function working on the first split but it works on the second.
Thanks!
In your loop you set firstSplit true if it hits a ( this happens in 2 places in your example, before x and right before "Hello"
you can fix this by setting firstSplit true and ignore the leading if ( before you beginning the loop. Then you allow the logic you have to handle the rest.
I also notice you dont have any logic that references t[3] right now.
That all said you really should use a pattern to parse something like this.
local function splitIfStatement(str)
t = {str:match("if%s*%((%w+)%s*[=<>]+%s*(%d+)%)%s*{(.+)}")}
return t
end
this pattern is very narrow and expects a specific type of if statement, you can learn more about lua patterns here: Understanding Lua Patterns
If the input is of the form
if (AAA == BBB){ CCC("Hello") }
with possible whitespace around the fields in question, then this code works:
S=[[if (x == 2){ output("Hello") } ]]
a,b,c = S:match('%(%s*(.-)%s.-%s+(.-)%)%s*{%s*(.-)%(')
print(a,b,c)

Progress forwards and backwards through table

first time here on Stack Overflow but definitely have found a lot of useful information here!
Currently I'm trying to figure out how to select the next item, or previous item in a table based on the current selection.
My current table is as follows:
maleSkins = { 7,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,32,33,34,35,
36,37,43,44,45,46,47,48,49,51,52,57,58,59,60,61,66,67,72,73,80,82,83,
84,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,
112,113,114,115,116,117,118,120,121,122,123,124,125,126,127,128,132,
133,134,135,136,137,142,143,144,146,147,153,154,156,159,160,161,162,
168,170,173,174,175,176,177,179,180,181,182,183,184,185,186,187,188,
189,200,202,203,204,206,210,212,213,217,220,221,222,223,227,228,229,
230,234,235,236,239,240,241,242,247,248,249,250,252,254,258,259,260,261,262 }
femaleSkins = { 9,10,11,12,13,31,38,39,40,41,53,54,55,56,69,76,77,88,89,90,91,92,
93,129,130,131,138,139,140,141,145,148,151,152,157,190,191,192,193,195,
196,197,198,199,201,207,211,214,215,216,218,219,224,225,226,232,233,237,238,243,244,245,246,251,256,257 }
The default selection is "7" in the maleSkins table, and I will use the femaleSkins table when they choose "female" for their gender.
Currently my function call looks like this
function selSkin(button,state)
if button ~= "left" and state ~= "up" then
return
end
if source == createChar.maleButt then
femaleSkin = false
maleSkin = true
elseif source == createChar.femaleButt then
maleSkin = false
femaleSkin = true
end
if source == createChar.nextSkin then
if maleSkin == true then
newModel = table.concat(maleSkins,)
elseif femaleSkin == true then
end
elseif source == createChar.prevSkin then
if maleSkin == true then
elseif femaleSkin == true then
end
end
end
So, inside the "createChar.nextSkin" and "createChar.prevSkin" is where I'm trying to sort through the table based on the current skin, but I'm unsure of how to proceed.
I would love if someone could give me the building blocks to do this, and I will build the rest myself!
(Side note: Predefined variables I will be using to make this happen)
maleSkin = true
femaleSkin = true
curSkin = 7
newModel = nil
First of all welcome to stack overflow :)
A small thing I noticed: you have two variables to store if the character is male or female. This allows for 4 combinations, 2 of which make no sense. You could just as well only use one variable, say, maleSkin and when it's false, then you use the female skin instead. Alternatively, you could just do skinType = 'male' and skinType = 'female' (Lua interns strings, so this is just as fast as comparing integers)
now, unless you plan to have a few millions of skins or more, you can just iterate through the table to find the current skin and then use the previous one.
function skinOffset(skin, skinList, offset)
for i,current_skin in ipairs(skinList) do
if current_skin == skin then
return skinList[i + offset]
else
end
end
Now, to get the next skin you can do skinOffset(curSkin, maleSkins, 1) and for the previous one skinOffset(curSkin, femaleSkins, -1).
Additionally save the table index in a global var.
There are several ways, for instance skinIndex = 1, your code could look like this:
if source == createChar.nextSkin then
newModel = maleSkin and maleSkins[skinIndex + 1] or femaleSkins[skinIndex + 1]
elseif source == createChar.prevSkin then
newModel = maleSkin and maleSkins[skinIndex - 1] or femaleSkins[skinIndex - 1]
end
But you have to beware of skinIndex < 1 or > #maleSkins/#femaleSkins

How can find the missing bracket in this code?

When I run the code it tells me there's an error which is ')' expected near '=':
function restartLvl()
for i = 1, #balloonTexts do
display.remove(balloonTexts[i])
print ("restart level")
end
score.text = '0'
ballRemain.text = '3'
balloonText = {}
createBalloons(1, 3)
if (askUser.isVisible = true) then --this is the line where the error occured
askUser.isVisible = false
end
if (yesBtn.isVisible = true) then
yesBtn.isVisible = false
end
if (noBtn.isVisible = true) then
noBtn.isVisible = false
end
end
I don't know how it is still missing a ')', because I closed all the brackets.
= is the assignment operator, == is the operator to test equality. Change it to:
if (askUser.isVisible == true) then
askUser.isVisible = false
end
And all the others as well. The brackets () can be ommited for simplicity:
if askUser.isVisible == true then
askUser.isVisible = false
end
If the value is a boolean, you can also do this because all values that are not nil or false are treated as true.
if askUser.isVisible then
askUser.isVisible = false
end
This is not related to your answer but
I recommend you to use lua glider IDE because this type error can be detect well by using this IDE.

if statement not working in Lua for io.read

I'm trying to make a 'simple' Y/N answer choice thing. (That you saw all the time on old programs) But an If Statement I'm using doesn't seem to want to work. I even print out the variable and it is nowhere near what i want to compare yet it still passes it.
--Porgram Functions
function check()
--Local Variables
local num = 0
local loop = true
io.write("Continue? (Y/N):")
--User input
local input = io.read()
while(loop==true) do
if (input=="y" or "Y") then
print("Ok!")
loop = true
num = 1
elseif (input=="n" or "N") then
print("Fine...")
num = 2
else
print("Invalid Answser!")
loop = true
num = 0
end
end
print(input)
return(num)
end
print (check())
I would've written your function like this:
function check()
io.write("Continue? (Y/N): ")
answer = io.read()
while( not (answer == "Y" or answer == "N") ) do
io.write("Invalid Answer! Try again (Y/N): ")
answer = io.read()
end
if answer == "Y" then
print("Ok!")
return 1
else
print("Fine...")
return 2
end
end
print(check())
Some examples of its use:
Continue? (Y/N): Huh?
Invalid Answer! Try again (Y/N): N
Fine...
2
>Exit code: 0
>lua -e "io.stdout:setvbuf 'no'" "a.lua"
Continue? (Y/N): Huh?
Invalid Answer! Try again (Y/N): Y
Ok!
1
A working version of your code would be:
function check()
local num = 0
local loop = true
io.write("Continue? (Y/N):")
while(loop==true) do
--User input
local input = io.read()
if (input == "y" or input == "Y") then
print("Ok!")
num = 1
loop = false --we want to stop looping if input is valid
elseif (input == "n" or input == "N") then
print("Fine...")
num = 2
loop = false --we want to stop looping if input is valid
else
print("Invalid Answser!")
-- loop = true no need to set looping to true again
num = 0
end
end
return(num)
end
The changes made were:
Get the user input inside the while loop, this way if the input is invalid and the loop goes again the same logic behind getting the input is used, we don't have to code two cases for getting input; one outside the loop the other within. It also pauses execution when the loop starts again, this was what was producing all that output!
input == "y" or "Y" doesn't do what you think. Instead it evaluates to (input == "y") or ("Y"), what you want it input == "y" or input == "Y".
You needed to set loop to false when the input was either "y" or "Y" or "n" or "N", otherwise the loop would continue.
Fourthly setting the loop to true inside the loop is unnecessary, it begins as true, and the only change you can make is to set it to false. And since each of the conditions are mutually exclusive i.e input being "y" or "Y" mutually exclusive to input being "n" or "N" or it being neither "y" or "Y" or "n" or "N". You don't need to worry about it being set to false unless you wanted the loop to end.
local function check()
io.write"Continue? (Y/N): "
local ans, num = {y = 1, n = 2}
repeat
num = ans[io.read():lower()] or 3
io.write(({"Ok!\n","Fine...\n","Invalid Answer! Try again (Y/N): "})[num])
until num < 3
return num
end
print (check())

Resources