Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I do an Instance.new and insert an object inside of that part? [Solved]

I want to put an explosion inside of a Part that just got made.

p = Instance.new("Part",game.Workspace)
p.Name = "ScriptMadePart"
p.Anchored = true
p.Position = Vector3.new(-22, 11.7, 11)
wait(1)
e = Instance.new("Explosion", game.Workspace.ScriptMadePart)

I do have an Explosion occur. Although, it's in the middle of Game. I want the explosion to become a child of ScriptMadePart. I tried doing the ("Explosion",game.Workspace.ScriptMadePart) but didn't work. I know this must be simple, sorry.

2 answers

✓ Accepted answer

Since 'p' is your variable assigned to the part you just made, you could set the parent of the explosion to it.

e = Instance.new("Explosion", p)

UPDATE

What you should be concentrating on, concerning the position of the explosion, is the 'Position' property.

For example:

e.Position = p.Position

This will set the position of the explosion to the new part you've created.

3 comments

0

All I did when I tried what I think you mean was change line 6 to e = Instance.new("Explosion",p) I still got the same exact thing occurring where my explosion is not happening where my part is.

0

Updated.

0

Oh, Okay. Thank you.

Putting an explosions in a part does not make the explosions move to the part position. You have to set Explosion.Position to the part's position.

The correct code is:

local Part = Instance.new("Part",workspace)--// No need for game.Workspace, just use workspace. Tip from me to you ;)

Part.Name='ScriptMadePart';
Part.Anchored=true;
Part.CFrame=CFrame.new(-22,11.7,11);--// Use CFrame, it's more precise.

delay(1,function()--// Spawn a thread in the thread scheduler to make your script more proficient.

	local Explosion=Instance.new('Explosion',Part);
	Explosion.Position=Part.CFrame.p;--// This was what you were missing.

end);

By doing Explosion.Position=Part.CFrame.p( you could also do Explosion.Position=Part.Position ), you set the explosion's position to the part's position.

This discussion is preserved as it was. New answers, comments, and votes are closed.