I started learning how to code and made a script. What do you think?

I just started learning how to script with Lua yesterday, and I made a fun script in my free time.

> Script

game.Workspace.Part.BrickColor = BrickColor.new("Really red") -- Allows you to change the color of the brick.
        wait(5) -- Allows you to wait for a certain amount of time.
        game.Workspace.Part.BrickColor = BrickColor.new("Baby blue") -- Once the wait command is finished being executed, line 3 will be executed.
        wait(5) -- The same effect as line 2, except once you're done waiting, it won't execute line 3 again. It will execute line 5.
        game.Workspace.Part.Anchored = false -- Part from the Workspace is already anchored once you join the game, but once line 5 is executed, Part's "Anchored" properties will be set to false.
        wait(5)
        game.Workspace.Part.BrickColor = BrickColor.new("Alder") -- From this point, the Part's BrickColor properties are just changing colors every 1/10 of a second.
        wait(0.1)
        game.Workspace.Part.BrickColor = BrickColor.new("Artichoke")
        wait(0.1)
        game.Workspace.Part.BrickColor = BrickColor.new("Baby blue")
        wait(0.1)
        game.Workspace.Part.BrickColor = BrickColor.new("Cadet blue")
        wait(5)
        game.Workspace.Part.BrickColor = BrickColor.new("Really black") -- Once line 15 is executed, there will be no other line to execute. Therefore, the code is finished/complete once it reaches line 15.

> Video of what it does

code.mp4

The script allows the program to manipulate the properties of a Part.
Thanks to AlvinBlox for the scripting tutorials!

3 Likes

You need to give us access to the video all i see is

3 Likes

Oops. I’ll fix that! Thanks for letting me know.

1 Like

I fixed it. I think you can now watch it.

This is a great start!
I’d suggest learning how to use local next, to shorten your code so you aren’t writing the same long lines repeatly.
For exmaple, you can change

game.Workspace.Part.BrickColor = BrickColor.new("Baby blue")

to

local Part = game.Workspace.Part
Part.BrickColor = BrickColor.New("Baby blue")
3 Likes

Okay, thanks for the suggestion(s)! I’ll use this from now on.

1 Like

I used TheDevKing’s video series to learn how to script, amazing tutorials.

1 Like

I prefer Alvin Blox’s tutorial, and I hear peas factory has amazing beginner and advanced tutorials. The decking I hear got his knowledge from peas factory, but I still watch the devkings tutorials from time to time

Seems fine, to make it even better, I suggest using a while wait() loop and making a table to randomly choose one of the colors from the table

3 Likes

I guess that is correct, however proper terminology is also important, especially when talking to new scripters
They’re actually called variables, if you call them locals, it could seem as if that is only what local is used for, not trying to criticise you or be rude or anything, I’m new to scripting too✔️

1 Like

I would recommend making a function like,

local function Foo(Part, color, waitTime)
    Part.BrickColor = Color3.new(color)
    wait(waitTime)
    return true
end

That’s probably how i would do it, although you are just learning, kinda pains me with all the repetitiveness
Then you could just call Foo() with the parameters, with maybe a loop if you want to do the same things multiple times

Or you could easily just throw it in a for loop

3 Likes

You can watch Alvin Blox’s vids to better understand functions, parameters and all the basics you need to know! I as clueless on all of that stuff until I watched his vids, I recommend them! :ok_hand:

1 Like

I know I might get yelled at for what some would call a micro optimization but have you considered using what we call a for loop for when the yield is the same?

2 Likes

I belive if you want to loop this you can put at the top of your script. (I learned coding not too long ago so IDK if this is right but I think it is:

while true do

If thats wrong someone please correct me.

1 Like

This is not efficient way of doing it you are doing useless lines that will take lot of memory, You should use for loops

2 Likes

Golly, I just come back and see 14 replies. Thank you all for the feedback and suggestions in scripting! It helps a lot since I am a beginner!

Although this is an awesome start, there are some simple things that will make your scripting experience more fun, efficient, and will result in cleaner code. Taking a quick look at your code I can assume you had to copy and paste a lot (now trust me I have gone through this and you will love for loops when you start using them).

First of all, you should assign your part to a variable so you don’t need to keep on typing
game.Workspace.Part.

local Part = game:GetService("Workspace").Part

Secondly, instead of having to add new lines of code to add a new color, you should put them in a table! A table is object that can hold multiple values.

local Part = game:GetService("Workspace").Part
local Colors = {"Really red", "Baby blue"}

You can add as many colors as you want. Just add them into the array as I did above.

Then you should make a simple variable for the delay in-between each color.

local Part = game:GetService("Workspace").Part
local Colors = {"Really red", "Baby blue"}
local Delay = 1

Lastly you should use a for loop so you don’t have that copy and pasting issue (along with a while true loop so it never stops). You can find out more about for loops here.

local Part = game:GetService("Workspace").Part
local Colors = {"Really red", "Baby blue"}
local Delay = 1
while true do
	for _, color in pairs(Colors) do
		Part.BrickColor = BrickColor.new(color)
		wait(Delay)
	end
end

Hope this helped a bit. Trust me, you will treasure for loops soon. Anyways, feel free to reply or PM me with any questions. Happy coding!

2 Likes

Much easier way:

local colors = {"Really blue", "New Yeller", "Really red", "Really black"} -- Your Colors In The Table
local part = game.Workspace.Part


for color = 1, #colors do
    part.BrickColor = BrickColor.new(color)
    wait(1.5)
end
1 Like

Why #colors?

Sorry I a need scripter too.

#table means the number of items in the table.

1 Like