My first lines in lua!

So, I wanted to program it where if i clicked on the part it’d turn green and print hello in f9 (dev console)
and I did it! I programmed the second line myself and referred to some tutorials for the other lines.

local clickDetector = workspace.Part.ClickDetector 
   function onMouseClick ()
	workspace.Part.BrickColor = BrickColor.Green() 
	  print("Hello!")
	end

clickDetector.MouseClick:connect(onMouseClick)

I think I did it well for my first time programming

7 Likes

This is a pretty good start however, I’d thought I’d give you useful advice. Instead of .BrickColor, you can always decide on using workspace.Part.Color = Color3.fromRGB(v1,v2,v3) This will allow you to be using RGB instead of predetermined colors. You can also use other Color3 functions if you feel like it. Also, I wouldn’t recommend using :connect as it is depreciated. Try getting in the rhythm of using :Connect as this is the better alternative. Overall, great job for your first time programming and I would recommend using the Roblox Developer Hub to further improve your scripting. Now try using if statements!

5 Likes

Thanks! I will be sure to use your advice for future programming.
(edited as I had shaky hands)

1 Like

I’m going to change the script so it turns red 2 seconds after clicking it by using wait() and change the :connect to :Connect, i’ll also use RGB because I wanna test that out!

Taking things slowly is what will always lead to greatness. I remember fiddling with so many free models and Roblox gears while learning and I still barely feel like I don’t know much. Took me a few years to understand the syntax and layout and a few more to feel like I’ve grasped it; granted, I was barely 8 when I first began learning and my attention span was that of a goldfish.

Experiment with different code layouts and create your own style, that’s all i can say.
Good luck!

3 Likes

I attempted to do that, and keep getting errors and the script not working at all.

local clickDetector = workspace.Part.ClickDetector 
   function onMouseClick ()
	wait(2)
	workspace.Part.Color = Color3.fromRGB(12,99,12)
 print("Hello!")
while workspace.Part.Color do Color3.fromRGB(99,0,0)
print("Making it Red!")

end

clickDetector.MouseClick:Connect(onMouseClick)

The while workspace.Part.Color do is causing the error, instead you need to set the color to 12,99,12, wait two seconds, then set the color to red. I suggest you try to write pseudocode to understand what you’re trying to write better.

When ClickDetector Pressed:
Set Color To 12,99,12
Print Hello
Print Making It Red
Wait Two Seconds
Set Color To 99,0,0
2 Likes

Thank you! I’ll try doing pseudocode so I can understand my problems better.

1 Like