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
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
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!
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
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.