Feedback on my first Instance.new code

Hi, I am very new to coding on ROBLOX and was wondering is there anything I
could improve on my instance.new script, thanks!

The code spawns in random sized balls with fire and a red color, I tried to make it like an asteriod.
I have considered trying to make it more simplfied, or not messy.
I want to try improve code by making it tidy and more simple or better alternative.

local Part = script.Parent 
local IsOn = false
local Prompt = script.Parent.ProximityPrompt

Prompt.Triggered:Connect(function()
			while true do 
		wait(0.2)
		local X = math.random(-1022, 1022)
		local Y = 100 
			local Z = math.random(-1020, 1022)
			
			local Size1 = math.random(1,55.2)

		
			local Block = Instance.new("Part") -- parrt
			Block.Size = Vector3.new(Size1, Size1, Size1)
		Block.Shape = Enum.PartType.Ball
		Block.Parent = game.Workspace:WaitForChild("Folder")
			Block.Position = Vector3.new(X, Y, Z)
			Block.Color = Color3.new(0.666667, 0, 0)
			local Fire = Instance.new("Fire",Block)
		end 
	end

Balls have to be the same size on each axis by the way. And there’s no Size2 and Size3 variables.

1 Like

That’s pretty good! The only recommendation I have to improve your code’s tidiness is to make sure your indentations are consistent; it helps with the readability.
For example:

function()
	while true do 
		local foo = true
		if foo then
			print("hello")
		else
			print("bye")
		end
	end
end 
2 Likes

I know these tips may not be important, but it saves whitespace. When you first say

local Block = Instance.new("Part")

There, you should state where the parent should be, like you did with the Fire instance. (Not sure if not stating the parent was intended, but) Like this:

local Block = Instance.new("Part",game.Workspace:WaitForChild("Folder"))

Also, instead of saying

while true do

you could say

while wait(0.2) do

and take out the wait(0.02) under it

Other than that (besides the indenting as @ProgramadorMan has stated earlier), it looks good!

1 Like

I am pretty sure that the second property of Instance.new() is depcricated.

3 Likes

Hmm, I didn’t know that. Thanks for telling me!

1 Like

Just a tip, never use the second argument of Instance.new() eg:

Instance.new("Part", workspace)

Bassically its bad for performance so you should always set the parent last like so:

local part = Instance.new("Part")
part.Color = Color3.fromRGB(255, 255, 255)
part.Shape = Enum.PartType.Ball
part.CFrame = CFrame.new(0, 10, 0)
part.Parent = workspace -- then set the parent after everything
1 Like

It’s not deprecated, just advised against if you’re setting properties. While that might seem pedantic at first, it’s important here because the original post has the same issue as using the second argument. (The post you linked even mentions that!)

This will set the Position and Color after the Block is parented to workspace, causing physics and render stuff to need to be redone. I don’t know the technical details, but in essence it’s better to instantiate any properties before putting something in the DataModel, because it’ll just be able to load the part, not load the part and then change things according to each property.

It’s fine for the Fire (only place OP actually uses the second argument) since he’s not setting any properties after, but you’re right that he should be setting properties first. That’s not unique to using the second argument, though; see my point above.

4 Likes

Yeah its fine if your not setting any properties afterwards but try setting the parent at the end when you are.

1 Like