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

How do I make both parts work at the same time after clicking the button (like a DVD player)?

I asked a question about the cover and things. Now I added a slot for it. But there was a problem for that. I want it where when I click it the cover and slot does the function like at the same time I should say. It is like a dvd player, where you click the button and the cover opens and slides the CD insert out. This is the script (Looks confusing):

--Combined script
local button = game.Workspace.Button
local slot = game.Workspace.Radio.Slot
local cover = game.Workspace.Radio.Cover
local home = cover.CFrame
debounce = false

function onClicked(button) --Supposed to work after you click the button and both the slot and cover come out at the same time
for t = 0, 4.8, 0.03 do
    wait(0.001)
    local v = math.cos(t / 4.8 * math.pi) * 45 + 90 + 45
    cover.CFrame = home
    cover.Rotation = Vector3.new(v, 0, 180)
end
end

local positionZValues = {-5.535, -6.635, -7.365} -- Z Value for Position (I wanted it to be from -5.535 to -7.365)
    if not debounce then
        debounce = true
        for a,v in pairs(positionZValues) do --This "for" loop runs through each value of the table
            slot.Position = Vector3.new(0, 0, v) -- Only for Z value
            wait(.001)
        end
        debounce = false
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)


How I made it? Well, I got the cover script that someone made by shortening my script out, but then I combined it, which I kind of made it confusing to me. But I hope you guys understand. Sorry for the trouble.

1 answer

✓ Accepted answer

Roblox has a built in function called spawn() we can use. Any function called by spawn()executes on a separate thread, meaning the script does not wait for it to finish running.

Here is your script, but modified to use spawn()

-- Combined script
local button = game.Workspace.Button
local slot = game.Workspace.Radio.Slot
local cover = game.Workspace.Radio.Cover
local home = cover.CFrame
debounce = false

function onClicked(button) --Supposed to work after you click the button and both the slot and cover come out at the same time
    spawn(function()
        for t = 0, 4.8, 0.03 do
            wait(0.001)
            local v = math.cos(t / 4.8 * math.pi) * 45 + 90 + 45
            cover.CFrame = home
            cover.Rotation = Vector3.new(v, 0, 180)
        end
    end)
    
    local positionZValues = {-5.535, -6.635, -7.365} -- Z Value for Position (I wanted it to be from -5.535 to -7.365)
    if not debounce then
        debounce = true
        for a = 1,16 do --This "for" loop runs through each value of the table
            slot.CFrame = slot.Position + Vector3.new(0, 0, -0.114375) -- Only for Z value
            wait(.01)
        end
        debounce = false
    end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
---0.114375

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