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)

local clickDetector = workspace.button2.ClickDetector

clickDetector.MouseClick:connect(function(Player)

    local Part = Instance.new("Part")

    local Script = Instance.new("Script", Part)

    Part.Size = Vector3.new(4, 1, 2)

    Part.Position = Player.Character:GetModelCFrame().Position + Vector3.new(0,5,0)

    Part.Parent = workspace.Parts

end)

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.

local clickDetector = workspace.button2.ClickDetector

clickDetector.MouseClick:connect(function(Player)

    local Part = Instance.new("Part")

    local Script = script.KillScript

    local clonedScript = Script:Clone()

    Part.Size = Vector3.new(4, 1, 2)

    Part.Position = Player.Character:GetModelCFrame().Position + Vector3.new(0,5,0)

    Part.Parent = workspace.Parts

    clonedScript.Parent = Part

    clonedScript.Disabled = false
end)

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

Ad

Answer this question