Skip to content
The Chat Archive
claude-opus-4-20250514-thinking-16k· Anthropic LMArena Human Preference 140K

Make a script for Lua that spawns a part when a button is clicked

  1. User

    Make a script for Lua that spawns a part when a button is clicked

  2. Assistant

    Here's a Lua script for Roblox that spawns a part when a button is clicked:

    -- This script goes inside a part that will act as your button
    
    local button = script.Parent -- The part this script is inside
    local clickDetector = button:FindFirstChild("ClickDetector")
    
    -- Create a ClickDetector if it doesn't exist
    if not clickDetector then
        clickDetector = Instance.new("ClickDetector")
        clickDetector.Parent = button
    end
    
    -- Function to spawn a new part
    local function spawnPart()
        -- Create a new part
        local newPart = Instance.new("Part")
        
        -- Set part properties
        newPart.Name = "SpawnedPart"
        newPart.Size = Vector3.new(4, 4, 4)
        newPart.Material = Enum.Material.Neon
        newPart.BrickColor = BrickColor.Random() -- Random color each time
        
        -- Position the part above the button
        newPart.Position = button.Position + Vector3.new(0, 10, 0)
        
        -- Parent the part to workspace
        newPart.Parent = workspace
        
        -- Optional: Make the part disappear after 5 seconds
        game:GetService("Debris"):AddItem(newPart, 5)
    end
    
    -- Connect the function to the click event
    clickDetector.MouseClick:Connect(spawnPart)
    

    Setup Instructions:

    1. Insert a Part into your workspace (this will be your button)
    2. Name it something like "SpawnButton"
    3. Insert a Script inside the part
    4. Paste the code above into the script

    What the script does:

    • Creates a ClickDetector if one doesn't exist
    • When clicked, spawns a new part 10 studs above the button
    • Each spawned part has a random color
    • Parts automatically disappear after 5 seconds (optional feature)

    To customize:

    • Change Vector3.new(4, 4, 4) to adjust the size
    • Change Vector3.new(0, 10, 0) to adjust spawn position
    • Remove the Debris:AddItem line if you want parts to stay permanently
    • Change BrickColor.Random() to a specific color like BrickColor.new("Really red")

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.