Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

[ANSWERED] When to use instance.new() and parent during scripting (with explanation)?

Asked by 4 years ago
Edited 4 years ago

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.

0
Forgive me if the question I asked was too long, it was my first time posting a question. monamidigger1 17 — 4y
0
not at all themaxogamer 58 — 4y

2 answers

Log in to vote
3
Answered by 4 years ago
Edited 4 years ago

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

0
in shortly you can add like that: local Part = Instance.new("Part", workspace) boshjio15854 15 — 4y
0
Yes, you can but, it causes performance problems, that's why you do it this way. GoreDemons 215 — 4y
0
For Parent, must we add game. For example, Part.Parent = game.Workspace So, is the additional "game." necessary? monamidigger1 17 — 4y
0
Say for example, your using ReplicatedStorage, you have to use game.ReplicatedStorage. But for workspace, you can do workspace or game.Workspace. GoreDemons 215 — 4y
0
Thank you monamidigger1 17 — 4y
Ad
Log in to vote
-1
Answered by 4 years ago

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

0
who downvoted :0 themaxogamer 58 — 4y

Answer this question