Is it possible to use an If statement inside of an If statement?

I’m trying to make a sort of money script where depending on what area a player is in, they will have to pay a different amount. The thing is they have to pay a different amount depending on what type of job they are working. I haven’t used if statements too much so I don’t know if it doesn’t work because I’m doing something wrong or if it just is not possible.

I tried to use this If statement:

if string.value == "area" then
     if string.value == "job" then
cost.value = 41
3 Likes

yes this will work, however a string cannot be rqual to 2 different things at once, I recommend using elseif for this

1 Like

This is possible, but the code you provided is syntactically incorrect. Each if statement must have an end after each block, e.g.:

-- do note that wrapping all conditions with parenthesis is
-- just my stylistic choice. you can ignore the parenthesis
-- unless necessary.
if (string.Value == "area") then
	if (string.Value == "job") then
		cost.Value = 41
	end
end

You can also match multiple conditions in one statement:

-- if the value is both "area" and "job".
if (string.Value == "area" and string.Value == "job") then
	cost.Value = 41
end

However, as @ProtectpocketAgain has stated a string value cannot be two different strings, and you should use an elseif block after the first if block, like so:

if (string.Value == "area") then
	-- place your "area" code here
elseif (string.Value == "job")  then
	cost.Value = 41
end
1 Like

Oddly enough, the code you provided appears to not work and does not give me an error code or anything.

why use 2 if’s when you can use
and

if string.value == "area" and string.value == "job" then

end

I was originally using the 2 Ifs because I wanted to make it so it would detect what area the player is in and depending on that area, the multiple jobs there would pay differently compared to different areas.

if area=="areaName" then
  if job=="doctor" then
  
  elseif job=="driver" then
   --if you want to check other job names use elseif
  else
  --everything else
  end
end

when you open an if, make sure to close it with an end.

Even after closing and doing the steps, it won’t work and doesn’t print any error.

you should check output for errors

After making a lot of edits to the script with debugging added into the script, it worked after making it so the script reruns when the value of the area and job is changed. (also thanks to @Y_VRN for the middle code instead of using the two If statements)

1 Like

@Mystical_Un1verse

Here, lets do a quick go over on some good uses of if statements and how you likely want to consider them in the future

Also, you end an END statement somewhere.

--First let us clean your code up
-- I am going to assume string in this case is a reference to 
-- a stringValue Instance

-- We do this because every time you "." into something this 
-- actually costs some processing sure you don't feel it now 
-- but its good to get in the habit of reducing how often you lean on 
-- "."  Also it is better to declear an inner scopped
-- variable in general in lua
local currentStringValue = string.value

-- Try to minimize random numbers like this 
-- It seems clutter but just organize where you put it
-- and name it what it is.  This will make it so much 
-- easier to figure out what number meets what
-- we want to stay away from magic numbers
local JobPay = 41

if currentStringValue == "area" then  
   if currentStringValue == "job" then cost.value = 41 end
end

-- OK so I rewrote you code but how could we do this
-- better
--[[NOTES: This pattern use is called predicate blocking
1. It makes reading our logic easier and believe it or not 
will reduce the number of if checks in common case fail conditions
2. By wrapping our multitiered condition logic into a function we 
get 2 benefits ( look at that, that's 2 for 2)  we give it contextual
context and make it easier understand and we encapsilate 
so we have opportunity to reuse, even if we don't end up reusing
we should always be doing things that open that opportunity up
as it is the best mentality to keep. 
]]--
local function isValidJob(stringValue)
     if stringValue ~= "area" then return false end
     if stringValue ~= "job" then return false end
     return true
end

if isValidJob(string.Value) then
    cost.value = JobPay
end
-- since this is fairly un complicated and looks like a readable setnance
-- Formatting like this isn't so bad because when we look at it 
-- we can read it like a sentence
-- if isValidJob, then costvalue is set to JobPay
if isValidJob(string.Value) then cost.value = JobPay end

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.