How can i make a function with Else and If?

You can write the script down below
There is two options of the script type:
1—Function with else
2—A new code with “else , if and a fucntion”

Hi, can you provide more info of what you want? I don’t think your question is very clear.

A function with ‘else’ and ‘if’ would just literally be:

function Something()
   if condition then
   else
   end
end

Is that what you’re looking for, or something else?

ı just scripted something but its not worked
its this

if game.workspace.part.clickdetector.mouse1click:connect(function()
    game.workpsace.part.brickcolor = brickcolor.red()
else
game.workspace.part.brickcolor = brickcolor.black()

I just made it up :smiley: but ı really need a script that is similar to this algorithm

1 Like

It should be game.Workpsace.part.BrickColor = BrickColor.new("Really Red")

Did you forget to add a “then”? Maybe try this:

local Colour = "Black"

game.workspace.part.clickdetector.mouse1click:connect(function()
    if Colour == "Black" then
        game.workpsace.part.brickcolor = brickcolor.red()
        Colour = "Red"
    else
        game.workspace.part.brickcolor = brickcolor.black()
        Colour = "Black"
    end
end)
2 Likes

Make sure that you are using proper capitals where they should be when coding.
Here’s an example of a elseif clickdetector script that changes a parts Transparency when clicked:

      script.Parent.ClickDetector.MouseClick:Connect(function()
        if script.Parent.Transparency == 0 then
	       script.Parent.Transparency = 1                        
              elseif script.Parent.Transparency == 1 then
     	script.Parent.Transparency = 0
  end
end)

But if we use elseif code with transparency , the parts transparency will not change , cause its happening too fastly , can you change the transparency of the elseif script to the brick color? *ı think

why not just tween it then or loop it in a lerp loop

The transparency will change even if its happening fast, But if you wanted to add a cooldown to help prevent script breaking or spamming you could add in a local value.
As for the Color changing I made it work here:
Red’s RGB Value is 255,0,0
Black’s Value is 0,0,0 (you could change this a bit for different shades of black)
You would also need to make sure that the Color of the Part is either set to Red or Black with the correct numbers in order for the code to work:

           script.Parent.ClickDetector.MouseClick:Connect(function()
                if script.Parent.Color == Color3.fromRGB(255, 0, 0) then
	            script.Parent.Color = Color3.fromRGB(0, 0, 0)
          elseif script.Parent.Color == Color3.fromRGB(0, 0, 0) then
	  script.Parent.Color = Color3.fromRGB(255, 0, 0)
    end
 end)

okay thats more open , but ı meant
When we mouse click to the part it will change to blue
Else
it will change to red

Also if you wanted to make a cooldown, adding in a value like this for example:

 local cool = false

You can then add it in so it will not let a click work until the cooldown is over like so:

  local cool = false
  script.Parent.ClickDetector.MouseClick:Connect(function()
if cool == false then
	if script.Parent.Color == Color3.fromRGB(255, 0, 0) then
		cool = true
	script.Parent.Color = Color3.fromRGB(0, 0, 0)
elseif script.Parent.Color == Color3.fromRGB(0, 0, 0) then
		script.Parent.Color = Color3.fromRGB(255, 0, 0)
		wait(1)
		cool = false
	  end
 	end
end)

You Could Make any two colors work for this
Just swap out the 2 lines with the color with the RGB of a different color and it should work fine.
Red = (255,0,0)
Blue = (0,0,255)
Green = (0,255,0)
Black = (0,0,0)
White = (255,255,255)
etc.

But why just we using RGB , what happen if we use brickcolor.new() is it works right as RGB

Yes, you could use BrickColor if you wanted too (I just used rgb because you can use more shades and colors with it) But for brick color it would work like this:

   local cool = false
 script.Parent.ClickDetector.MouseClick:Connect(function()
if cool == false then
  cool = true
if script.Parent.BrickColor.Color == BrickColor.Black() then
	script.Parent.BrickColor.Color = BrickColor.Red()
elseif script.Parent.BrickColor.Color == BrickColor.Red() then
	script.Parent.BrickColor.Color = BrickColor.Black()
	wait(1)
	cool = false
  end
 end
end)
1 Like

It is the same thing, developers just tend to argue over it like workspace or game:GetService(“Workspace”)

thanks for this information do you know? ı just always wondered about the meaning of this
ı meant
GetService

basically get service will ensure that it will work if you change the name of workspace or any service in explorer. however, who is doing that?