I was learning the basics of Lua recently and I discovered instance.new() and Parent. The video I was looking on Youtube and the youtuber did not explain instance.new() and Parent. I searched in YouTube and searched the web but I did not get anything. Can somebody please explain to me when to use instance.new() and Parent with explanation please. Thank you.
Instance.new()
is a Rlua function which is used to make new Objects / Scripts inside of the game, for example:
local Part = Instance.new("Part")
.Parent
is a property of almost any object / script, it determines where the script is placed, for example inside a model is a part, it's parent is the model.
Using Parent
:
local Part = Instance.new("Part") Part.Parent = workspace -- makes part in workspace
This can be used to make parts at specific locations using CFrame
:
local Part = Instance.new("Part") Part.Parent = workspace -- makes part in workspace Part.CFrame = CFrame.new(1,1,1)
EDIT
You can use this:
local Part = Instance.new("Part", workspace)
However, this causes performance issues
Basicaly instance.new() is used to insert a part ingame from a script lets say you have a part called ball and you wanna spawn it from script
you just gonna haveto put your part somewere lets say in replicated storage
first you will haveto find replicated storage so it goes like this
local rp = game.ReplicatedStorage
now we know where replicated storage is so we haveto find the part
local ball = rp.ball
now we know that ball is in replicated storage
so we can just type
instance.new(ball.game.workspace)
and that will spawn your part in game
now parents and childs is more easy
up there when we typed local
ball = rp.ball
that means that balls parent is replicated storage
we can also do like lets say you have a folder in workspace and inside that folder you have 2 parts
first part we will call it part1 and part2
and we also have a script inside
so now we haveto try to find the parts
we haveto find the folder first, we know that script is inside folder so that means that folder is scripts parents so it goes like this
local folder = script.parent
now we know where folder is so we gonna try to find the part1 or 2 it will go like this
local part1 = folder.part1
so that . means that its inside the folder
i hope you got everything right ask me any questions