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

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

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

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.

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