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

How do you spawn a part with a pre-made script inside it?

Asked by 3 years ago

i'm starting to practice scripting and one of the first tasks i've set myself is to make a setup where you press a button, a part spawns above your head and when it hits you it makes a bonk sound (comedy i know)

01local clickDetector = workspace.button2.ClickDetector
02 
03clickDetector.MouseClick:connect(function(Player)
04 
05    local Part = Instance.new("Part")
06 
07    local Script = Instance.new("Script", Part)
08 
09    Part.Size = Vector3.new(4, 1, 2)
10 
11    Part.Position = Player.Character:GetModelCFrame().Position + Vector3.new(0,5,0)
12 
13    Part.Parent = workspace.Parts
14 
15end)

in line 7 there you can see i figured out how to put a blank script inside the part, but how would i put a script with code into the part?

the code is just the check for humanoid then sound play, that part works

thanks

1 answer

Log in to vote
1
Answered by
3F1VE 257 Moderation Voter
3 years ago

Make a script parented to the current script, have it disabled and clone it and un disable it.

01local clickDetector = workspace.button2.ClickDetector
02 
03clickDetector.MouseClick:connect(function(Player)
04 
05    local Part = Instance.new("Part")
06 
07    local Script = script.KillScript
08 
09    local clonedScript = Script:Clone()
10 
11    Part.Size = Vector3.new(4, 1, 2)
12 
13    Part.Position = Player.Character:GetModelCFrame().Position + Vector3.new(0,5,0)
14 
15    Part.Parent = workspace.Parts
16 
17    clonedScript.Parent = Part
18 
19    clonedScript.Disabled = false
20end)

Contact me on discord (dimondflash57#0001) if you have any other questions.

Ad

Answer this question