I’m currently struggling with this bit of code in one of my scripts.
Essentially, my problem is within this elseif statement (and a few others in the script do exactly what this one is doing), adding an extra end is making studio spit out errors like:
“Error: (54,3) Expected ‘)’ (to close ‘(’ at line 7), got ‘missionFrame’”
I couldn’t post this into the Studio Bugs section of Platform Feedback so the next best place I could post it was here, sorry.
If this isn’t a bug, what am I doing wrong here? Help would be greatly appreciated!
Cheers.
Better formatted (thanks jorito)
The solution you mentioned at the bottom of the post actually solved my issue, thank you for noticing this!
It looks like you have unmatched parentheses somewhere from that error. You should check if you open any at line 7 that are not correctly closed or post the whole script here so we can take a look.
last reply was deleted due to a formatting error, my bad.
Even after asking God himself, I couldn’t figure out what’s wrong in the script.
'Scuse the fairly messy look of it, I tend to clean up my scripts when they’re finished, this problem happens with every elseif statement within this function.
missionStart.OnClientEvent:connect(function(currentMission)
if currentMission == "ParkMission" then
missionText.Text = "An event is due to start at the park. Be the first to deliver the ice cream!"
end
if currentMission == "BumpTowerMission" and isNight.Value == false then
missionText.Text = "Bump Tower Media Entertainment are holding a conference and have requested an ice cream delivery. Be the first to get there!"
elseif isNight.Value == true then
missionText.Text = "Bump Tower Media Entertainment directors are in a late night meeting and have ordered some ice cream. Get there as quickly as possible!"
end
if currentMission == "ChineseMission" then
missionText.Text = "The Chinese restaurant needs ice cream for their desserts. Get there quick!"
end
if currentMission == "CinemaMission" then
missionText.Text = "The Cinema has put out an order for ice cream. Will you be the first to deliver? Go go go!"
end
if currentMission == "MayorMission" then
missionText.Text = "The Mayor's staff are taking a break and fancy some ice cream. It's hot and they're desperate to cool down, hurry!"
end
if currentMission == "HospitalMission" and isNight.Value == false then
missionText.Text = "The doctors at the Hospital want to treat the childrens ward with suprise ice cream. Let's not dissapoint them!"
elseif isNight.Value == true then
missionText.Text = "The Hospital staff have ordered ice cream for their night shift breaks. Be the first to get it to them!"
end
if currentMission == "BurgerBarMission" then
missionText.Text = "The Burger Bar has run out of ice cream and needs a delivery fast. Pedal to the metal time!"
end
if currentMission == "NachoMission" then
missionText.Text = "The Nacho restaurant are calling around for a speedy ice cream delivery. You're the speedy one, right? Get to it!"
end
if currentMission == "PizzaParlourMission" then
missionText.Text = "The Pizza Parlour are requesting an ice cream delivery. Be the first to get there!"
end
if currentMission == "LesiureCenterMission" then
missionText.Text = "The Lesiure Center needs some ice cream for its cafe. Those fitness fanatics are hungry, get moving!"
end
if currentMission == "PancakeBarMission" then
missionText.Text = "The Pancake Bar is running low on ice cream. Be fast and be the first!"
end
if currentMission == "ShoppingCenterMission" and isNight.Value == false then
missionText.Text = "A new shop is opening up at the Mall and wants to give out free ice cream. Be the first to deliver!"
elseif isNight.Value == true then
missionText.Text = "The Mall is open for late night shopping and customers are requesting ice cream. Deliver it first!"
end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--v-- This code shouldn't be part of the elseif statement, but it puts itself in the elseif statement, regardless of where the end is positioned.
missionFrame:TweenPosition(UDim2.new(0.25, 0, 0.25, 0), "Out", "Bounce", 1.75)
wait(8)
missionFrame:TweenPosition(UDim2.new(0.25, 0, -1, 0), "In", "Back", 0.75)
end)
local missionStart = game.ReplicatedStorage.MissionStart
local missionFrame = script.Parent.Parent
local missionText = script.Parent
local isNight = game.Workspace.DayNightCycleValues.IsNight
missionStart.OnClientEvent:connect(function(currentMission)
if currentMission == "ParkMission" then
missionText.Text = "An event is due to start at the park. Be the first to deliver the ice cream!"
end
if currentMission == "BumpTowerMission" and isNight.Value == false then
missionText.Text = "Bump Tower Media Entertainment are holding a conference and have requested an ice cream delivery. Be the first to get there!"
elseif isNight.Value == true then
missionText.Text = "Bump Tower Media Entertainment directors are in a late night meeting and have ordered some ice cream. Get there as quickly as possible!"
end
if currentMission == "ChineseMission" then
missionText.Text = "The Chinese restaurant needs ice cream for their desserts. Get there quick!"
end
if currentMission == "CinemaMission" then
missionText.Text = "The Cinema has put out an order for ice cream. Will you be the first to deliver? Go go go!"
end
if currentMission == "MayorMission" then
missionText.Text = "The Mayor's staff are taking a break and fancy some ice cream. It's hot and they're desperate to cool down, hurry!"
end
if currentMission == "HospitalMission" and isNight.Value == false then
missionText.Text = "The doctors at the Hospital want to treat the childrens ward with suprise ice cream. Let's not dissapoint them!"
elseif isNight.Value == true then
missionText.Text = "The Hospital staff have ordered ice cream for their night shift breaks. Be the first to get it to them!"
end
if currentMission == "BurgerBarMission" then
missionText.Text = "The Burger Bar has run out of ice cream and needs a delivery fast. Pedal to the metal time!"
end
if currentMission == "NachoMission" then
missionText.Text = "The Nacho restaurant are calling around for a speedy ice cream delivery. You're the speedy one, right? Get to it!"
end
if currentMission == "PizzaParlourMission" then
missionText.Text = "The Pizza Parlour are requesting an ice cream delivery. Be the first to get there!"
end
if currentMission == "LesiureCenterMission" then
missionText.Text = "The Lesiure Center needs some ice cream for its cafe. Those fitness fanatics are hungry, get moving!"
end
if currentMission == "PancakeBarMission" then
missionText.Text = "The Pancake Bar is running low on ice cream. Be fast and be the first!"
end
if currentMission == "ShoppingCenterMission" and isNight.Value == false then
missionText.Text = "A new shop is opening up at the Mall and wants to give out free ice cream. Be the first to deliver!"
elseif isNight.Value == true then
missionText.Text = "The Mall is open for late night shopping and customers are requesting ice cream. Deliver it first!"
end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--v-- This code shouldn't be part of the elseif statement, but it puts itself in the elseif statement, regardless of where the end is positioned.
missionFrame:TweenPosition(UDim2.new(0.25, 0, 0.25, 0), "Out", "Bounce", 1.75)
wait(8)
missionFrame:TweenPosition(UDim2.new(0.25, 0, -1, 0), "In", "Back", 0.75)
end)
Weirdly, I’m not getting any errors with that code and everything looks correct. Are you still getting errors with it and is there any possibility the errors are coming from another script?
I don’t get errors in the output as such, just complaints from the script analysis window in studio if I put an extra end to close an elseif in an attempt to fix it.
The script runs incorrectly for me, not properly changing text when it’s supposed to because it thinks the code underneath each “elseif” is part of said “elseif”, causing unwanted effects, I did try restarting studio in case it was a one off bug, but it didn’t help.
I was going to post this to bug reporting but I couldn’t post a new topic there, so I posted here.
Oh, I misunderstood. In what circumstances are you trying to get the Tween to happen? Only when any other of the if statements are not hit? The tween happening every time is what is expected based on the current code.
Example of the control flow:
function doThing(x)
if x == 1 then
print("X is one")
elseif x == 2 then
print("X is two")
end
print("X might be anything, always prints")
end
The the code outside the if statement will always happen no matter which branch in the if statement is taken.
I think what you are going for is more like:
function doThing(x)
if x == 1 then
print("X is one")
elseif x == 2 then
print("X is two")
else
print("X is not one or two")
end
end
You can fix your current code by making an big if statement with one elseif clause for each possible value of currentMission. Then only do the tweening in the else clause at the end.
--v-- This code shouldn't be part of the elseif statement, but it puts itself in the elseif statement, regardless of where the end is positioned.
missionFrame:TweenPosition(UDim2.new(0.25, 0, 0.25, 0), "Out", "Bounce", 1.75)
wait(8)
missionFrame:TweenPosition(UDim2.new(0.25, 0, -1, 0), "In", "Back", 0.75)
This part of the code happens regardless of your if statements. It runs every time the remote is fired.
I didn’t change anything, but this is the script correctly formatted for those of you who can’t read it
local missionStart = game.ReplicatedStorage.MissionStart
local missionFrame = script.Parent.Parent
local missionText = script.Parent
local isNight = game.Workspace.DayNightCycleValues.IsNight
missionStart.OnClientEvent:connect(function(currentMission)
if currentMission == "ParkMission" then
missionText.Text = "An event is due to start at the park. Be the first to deliver the ice cream!"
end
if currentMission == "BumpTowerMission" and isNight.Value == false then
missionText.Text = "Bump Tower Media Entertainment are holding a conference and have requested an ice cream delivery. Be the first to get there!"
elseif isNight.Value == true then
missionText.Text = "Bump Tower Media Entertainment directors are in a late night meeting and have ordered some ice cream. Get there as quickly as possible!"
end
if currentMission == "ChineseMission" then
missionText.Text = "The Chinese restaurant needs ice cream for their desserts. Get there quick!"
end
if currentMission == "CinemaMission" then
missionText.Text = "The Cinema has put out an order for ice cream. Will you be the first to deliver? Go go go!"
end
if currentMission == "MayorMission" then
missionText.Text = "The Mayor's staff are taking a break and fancy some ice cream. It's hot and they're desperate to cool down, hurry!"
end
if currentMission == "HospitalMission" and isNight.Value == false then
missionText.Text = "The doctors at the Hospital want to treat the childrens ward with suprise ice cream. Let's not dissapoint them!"
elseif isNight.Value == true then
missionText.Text = "The Hospital staff have ordered ice cream for their night shift breaks. Be the first to get it to them!"
end
if currentMission == "BurgerBarMission" then
missionText.Text = "The Burger Bar has run out of ice cream and needs a delivery fast. Pedal to the metal time!"
end
if currentMission == "NachoMission" then
missionText.Text = "The Nacho restaurant are calling around for a speedy ice cream delivery. You're the speedy one, right? Get to it!"
end
if currentMission == "PizzaParlourMission" then
missionText.Text = "The Pizza Parlour are requesting an ice cream delivery. Be the first to get there!"
end
if currentMission == "LesiureCenterMission" then
missionText.Text = "The Lesiure Center needs some ice cream for its cafe. Those fitness fanatics are hungry, get moving!"
end
if currentMission == "PancakeBarMission" then
missionText.Text = "The Pancake Bar is running low on ice cream. Be fast and be the first!"
end
if currentMission == "ShoppingCenterMission" and isNight.Value == false then
missionText.Text = "A new shop is opening up at the Mall and wants to give out free ice cream. Be the first to deliver!"
elseif isNight.Value == true then
missionText.Text = "The Mall is open for late night shopping and customers are requesting ice cream. Deliver it first!"
end
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--v-- This code shouldn't be part of the elseif statement, but it puts itself in the elseif statement, regardless of where the end is positioned.
missionFrame:TweenPosition(UDim2.new(0.25, 0, 0.25, 0), "Out", "Bounce", 1.75)
wait(8)
missionFrame:TweenPosition(UDim2.new(0.25, 0, -1, 0), "In", "Back", 0.75)
end)
I did notice that on the BumpTowerMission, if it is night, it will ALWAYS fire that way. The correct way to do this is:
if currentMission == "BumpTowerMission" then
if isNight.Value == false then
missionText.Text = "Bump Tower Media Entertainment are holding a conference and have requested an ice cream delivery. Be the first to get there!"
elseif isNight.Value == true then
missionText.Text = "Bump Tower Media Entertainment directors are in a late night meeting and have ordered some ice cream. Get there as quickly as possible!"
end
end
This is the same with the Hospital and ShoppingCenter mission.
Also, (Slightly unrelated) Your elseif statements where it says:
this will run even if you fire the remote event with no currentMission variable attached. You have 3 of these in your script and could potentially run the wrong code for the scenario that you selected. Instead, keep with what you have done and use the elseif statements like this:
elseif currentMission == "WhateverMission" and isNight == false/true then
End
One other way to improve code like this is to separate data from code. In this case you would keep the mission descriptions in a table indexed by the mission name and then have a lookup function to get the mission description.
This cuts down on the amount of code and makes it easier to see what is going on. E.g:
local MissionDescriptionNight = {
["BumpTowerMission"] = "Bump Tower Media Entertainment directors are in a late night meeting and have ordered some ice cream. Get there as quickly as possible!",
----More mission descriptions.
}
local MissionDescriptionDay = {
["ParkMission"] = "An event is due to start at the park. Be the first to deliver the ice cream!",
-- More mission descriptions.
}
function getMissionText(currentMission)
if isNight.Value and MissionDescriptionNight[currentMission] then
return MissionDescriptionNight[currentMission]
end
return MissionDescriptionDay[currentMission]
end
missionStart.OnClientEvent:connect(function(currentMission)
local text = getMissionText(currentMission)
if text then
missionText.Text = text
else
missionFrame:TweenPosition(UDim2.new(0.25, 0, 0.25, 0), "Out", "Bounce", 1.75)
wait(8)
missionFrame:TweenPosition(UDim2.new(0.25, 0, -1, 0), "In", "Back", 0.75)
end
end)
This way you can add and remove mission descriptions without having to worry about introducing bugs.
Cheers to everyone who replied and helped me sort this issue, I managed to get the problem fixed.
Also thank you @TheGamer101, you showed me some pretty good tips to optimize this script and future ones with that variable method, I didn’t know that was possible.
You can also store them as numbers or IDs so spelling errors will not mix up missions and you can see all the available missions in one location
it might also make it easier to read or find than “…Mission”
i.e. in a module:
local Mission = {
BumpTower = 0,
Park = 1,
MyMyssion = 2
}
return Mission
and in code
local Mission = require(missions)
print(Mission.Park)
print(Mission.Park==Mission.Park)
I typically do this for readability and debugging. It also conveniently shows in autocomplete (will show the missions while typing them)
The autocomplete is a bit finicky. The easiest way to get it to work is instead of using wait for child and getservice in the require, use it like this:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MissionModule = ReplicatedStorage:WaitForChild("Mission")
local Mission = require(game.ReplicatedStorage.Mission)
I cant seem to get it to work without a direct reference
May be fixed or improved in a later update but this should work for now