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
while true do
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)
Just a tip, never use the second argument of Instance.new() eg:
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.