Elseif statements assistance

Hello! I need some assistance with using elseif statements. I am still new to scripting, and my current ‘project practice task’ is having a banana or an apple fall into a cup, creating juice, as well as the button being clicked ‘animate’ going down and back up. The original script I had with the singular fruits falling into the cup went like this;

local function Onclick()
script.Parent.Transparency = 1
game.Workspace.PushedButton.BrickColor = BrickColor.new(“Lime green”)
game.Workspace.Apple.Anchored = false
game.Workspace.Apple.CanCollide = true
game.Workspace.Juice.Transparency = 0
wait (0.5)
script.Parent.Transparency = 0
end

script.Parent.ClickDetector.MouseClick:Connect(Onclick)

The script.Parent had another part underneath it that would become visible once script.Parent would become transparent. This worked, heres a short video of my success. https://gyazo.com/9ff6fbd62dec11a043c54a33d51dff44

I was trying to advance more, and have 2 fruits go into the cup and mix together once inside into a colour (Red + Yellow = Orange)

This was the script, but it doesn’t seem to work. I’ve spent a while trying to mix and match it, but I don’t know how to set it up properly. I’ve tried multiple ideas that have come to mind, but it’s been unsuccesful so far.

When clicking the button to drop the apple:

local function Onclick()
script.Parent.Transparency = 1
game.Workspace.PushedButton.BrickColor = BrickColor.new(“Lime green”)
game.Workspace.Apple.Anchored = false
game.Workspace.Apple.CanCollide = true
game.Workspace.Juice.Transparency = 0
game.Workspace.Juice.BrickColor = BrickColor.new(“Bright Red”)
if game.Workspace.Juice.BrickColor == BrickColor(“New Yeller”) then
game.Workspace.Juice.BrickColor = BrickColor.new(“Neon orange”)
wait (0.5)
script.Parent.Transparency = 0

end

end

script.Parent.ClickDetector.MouseClick:Connect(Onclick)

Second button for the banana to drop:

local function Onclick()
script.Parent.Transparency = 1
game.Workspace.PushedButton.BrickColor = BrickColor.new(“Lime green”)
game.Workspace.Banana.Anchored = false
game.Workspace.Banana.CanCollide = true
game.Workspace.Juice.Transparency = 0
game.Workspace.Juice.BrickColor = BrickColor.new(“New Yeller”)
if game.Workspace.Juice.BrickColor == BrickColor(“Bright Red”) then
game.Workspace.Juice.BrickColor = BrickColor.new(“Neon orange”)
wait (0.5)
script.Parent.Transparency = 0

end

end

script.Parent.ClickDetector.MouseClick:Connect(Onclick)

When the fruits drop into the cup, starting with the apple, it drops in properly, but the brick becomes grey instead, and not Bright red. When I drop the Banana in, it drops in properly, but the brick becomes yellow, and I suspect that it’s because the dropped apple didn’t make the juice Bright red. I’m not sure what I’m doing wrong. When I re-click the Button that should initially drop in the apple, it turns the juice grey, and doing the same action with the button that drops the banana turns the juice Yellow. Here is a video of what it’s doing:
https://gyazo.com/e7ce46b97ad5500539b5c9376f79f243

I appreciate any replies ! If you also have any resources or links to read, or even people to watch, I’d appreciate that too!

1 Like

Lua is case-sensitive. This is where your issue resides.
It’s supposed to be BrickColor.new("Bright red").

I also noticed that you forgot to put the “.new” in some of your BrickColor.new(). It will error attempt to call a table value.

BrickColor.new(“Bright Red”) – isn’t a color, hence will return the default which is Medium stone grey.

Same goes to your second script.
if game.Workspace.Juice.BrickColor == BrickColor(“Bright Red”) then
to
if game.Workspace.Juice.BrickColor == BrickColor.new(“Bright red”) then

1 Like

Oh! The changing of the capital R has fixed the problem, yes. Now the buttons are just staying down, and being not what I would have wanted them to. I’ll try fix it myself too, but the colours of juice are just changing to whatever button I click. Here’s an example.

I initially wanted it to become orange if the banana was put in after the red, vice versa.

https://gyazo.com/80a6fabf8d1395fd5c70e7677c25037a

Ah, then you’d have to do some mix and matching logic here.

The first thing that came to my mind is to first gather what color the water(?) is, and then on a preset list of expected colors of what + what becomes. – you’ll have to do this because ROBLOX doesn’t simply have a :MixColor(color1, color2) function.

Will edit with a solution in a second, writing my own code.

Well on the first script, you are turning the apple into red juice, then in the code it is seeing if the juice is yellow? And if it is yellow, it turns the juice orange? Maybe that is the problem. Same for the other script.

Actually, have you fixed this?

These are the specific lines that you have to fix.

Yes! I have fixed them. It didn’t work, kept the same problem. (Buttons not working properly, not sure why anymore.) I’ll read over Mix matching function as well, and in general, all the functions. :slight_smile:

@Kamlkaze_Kid Ah, I’ll explain my thought process for this. I initially wanted it to go red like usual if the apple was put in. But, ‘if’ the juice is yellow as a result of the dropped banana, then it would turn the juice orange. Talking about it and what you have said and re-reading over it, I guess I have realised the problem now. It doesn’t mix because the script doesn’t know if the juice is bright red or not., it never specified. I sort of just wrote it as, ‘If the juice is yellow, then turn it orange’ without allowing it to detect a red source.

I’m going to re-write a few and edit this post to see if it works or not.

All you have to do is fix the problems @astra_wr and I mentioned, and it should work fine.

Ah, I’m sure that I see the issue now.

Your if statement will always return false because you’ve already changed the BrickColor before that.
What you can do is:

if game.Workspace.Juice.BrickColor == BrickColor.new(“New Yeller”) then
    game.Workspace.Juice.BrickColor = BrickColor.new(“Neon orange”)
else
    game.Workspace.Juice.BrickColor = BrickColor.new(“Bright red”)
wait (0.5)

and so forth, same goes to the other script.

This should solve your issue.

!! For some odd reason, it isn’t working, but the buttons have worked properly, except the red one just turns gree. Here is an example.
https://gyazo.com/fb24485f336419428abe4b81c1520a91

Could you send me your full code? (don’t seperate them)

Yes, definitely.

Apple:
local function Onclick()
script.Parent.Transparency = 1
game.Workspace.PushedButton.BrickColor = BrickColor.new(“Lime green”)
game.Workspace.Apple.Anchored = false
game.Workspace.Apple.CanCollide = true
game.Workspace.Juice.Transparency = 0
game.Workspace.Juice.BrickColor = BrickColor.new(“Bright red”)
if game.Workspace.Juice.BrickColor == BrickColor.new(“New Yeller”) then
game.Workspace.Juice.BrickColor = BrickColor.new(“Neon orange”)
else
game.Workspace.Juice.BrickColor = BrickColor.new(“Bright red”)
wait (0.5)
script.Parent.Transparency = 0

end

end

script.Parent.ClickDetector.MouseClick:Connect(Onclick)

Banana:

local function Onclick()
script.Parent.Transparency = 1
game.Workspace.PushedButton.BrickColor = BrickColor.new(“Lime green”)
game.Workspace.Banana.Anchored = false
game.Workspace.Banana.CanCollide = true
game.Workspace.Juice.Transparency = 0
game.Workspace.Juice.BrickColor = BrickColor.new(“New Yeller”)
if game.Workspace.Juice.BrickColor == BrickColor.new(“Bright red”) then
game.Workspace.Juice.BrickColor = BrickColor.new(“Neon orange”)
else
game.Workspace.Juice.BrickColor = BrickColor.new(“New Yeller”)
wait (0.5)
script.Parent.Transparency = 0

end

end

script.Parent.ClickDetector.MouseClick:Connect(Onclick)

Forgot to mention; please format your code by using ```code```.

I’ve rewritten your script and wrote what I changed, let me know if it’s working as intended now.

--Apple:
local function Onclick()
script.Parent.Transparency = 1
game.Workspace.PushedButton.BrickColor = BrickColor.new("Lime green")
game.Workspace.Apple.Anchored = false
game.Workspace.Apple.CanCollide = true
game.Workspace.Juice.Transparency = 0
--game.Workspace.Juice.BrickColor = BrickColor.new("Bright red") [REMOVED]
if game.Workspace.Juice.BrickColor == BrickColor.new("New Yeller") then
game.Workspace.Juice.BrickColor = BrickColor.new("Neon orange")
else
game.Workspace.Juice.BrickColor = BrickColor.new("Bright red")
end

wait(0.5) --[MOVED THIS OUT OF THE IF STATEMENT SCOPE, PROBABLY WHAT YOU WANTED. IF NOT; PUT IT BACK IN]
script.Parent.Transparency = 0
end

script.Parent.ClickDetector.MouseClick:Connect(Onclick)

--Banana:

local function Onclick()
script.Parent.Transparency = 1
game.Workspace.PushedButton.BrickColor = BrickColor.new("Lime green")
game.Workspace.Banana.Anchored = false
game.Workspace.Banana.CanCollide = true
game.Workspace.Juice.Transparency = 0
-- game.Workspace.Juice.BrickColor = BrickColor.new("New Yeller") [REMOVED]
if game.Workspace.Juice.BrickColor == BrickColor.new("Bright red") then
game.Workspace.Juice.BrickColor = BrickColor.new("Neon orange")
else
game.Workspace.Juice.BrickColor = BrickColor.new("New Yeller")
end

wait (0.5) --[MOVED THIS OUT OF THE IF STATEMENT SCOPE, PROBABLY WHAT YOU WANTED. IF NOT; PUT IT BACK IN]
script.Parent.Transparency = 0 

end

script.Parent.ClickDetector.MouseClick:Connect(Onclick)

!! Yes, it worked how I wanted it to. I will also remember that you can use code .

I appreciate the assistance. I will read over it a few times and see what else more I can add to advance my understanding on the script. Thank you very much. It was a simple script I wanted to learn how to do, and just didn’t know how to use the ‘else’ ‘if’ properly.

Glad I could help! Be sure to mark my post as your solution so anyone that views this thread can skip straight to the solution.

1 Like