How do I make a part re-appear in the workspace after a moment, after it was moved to ReplicatedStorage?

Sorry if the title is very confusing, it’s hard to explain in a single sentence.
I’ve made it so if you click on a part, it gets moved to ReplicatedStorage in 10 seconds. I want it to go back to the Workspace after a few seconds after it’s been moved. I tried several things to do that, but I just can’t get it to work.
I tried constantly checking if the part’s parent is ReplicatedStorage with a while true do loop, that didn’t work (I probably don’t know something about what I tried here), and I tried checking if the part’s parent is Workspace with a while true do loop, and if it isn’t, then do the thing I want to do, that didn’t work aswell.
Just to get it out of the way, I can’t just put a wait right after the script has moved the part to ReplicatedStorage, because I can’t trigger the mouseclick event again before the wait is done, and that is a problem.
Any help? Ask if you need more explanation on something.

1 Like
-- Click

wait(10)

Part.Parent = game.ReplicatedStorage

wait(10)

game.ReplicatedStorage.Part.Parent = workspace

Is the script inside the actual part?

2 Likes

I forgot to say, I’m using a tool. Whenever I click a certain part with the tool, it gets moved to ReplicatedStorage. I have several of these parts. The reason I’m not putting a wait after the part gets moved is, then I can’t click on another part to make it get moved, before the wait has ended, I don’t want that to happen.

Do you have discord? Maybe I can help your further if I can actually communicate with screenshare

So the solution to this issue:

  1. Make the function that moves the part to replicated storage and back synchronous by using spawn(), a coroutine, etc

  2. Or more trivially, replace

    wait(5)
    — move back to workspace

With

delay(5, function()
  — move part to workspace
end)

What’s your specific use case? What are you creating?

You’re capable of running this code in coroutines in order to effectively pseudothread or prevent the waits from affecting the thread from yielding for the initial click function. You may also use BindableEvents, which may be more preferable.

BindableEvent.Event:Connect(function (part)
    local originalParent = part.Parent
    part.Parent = game:GetService("ReplicatedStorage")
    wait(10)
    if originalParent then
        part.Parent = originalParent
    end
end)

-- When you click, simply:
BindableEvent:Fire(clickedPart)

The above is sample code for completing this with BindableEvents. Modifying it to fit your needs is up to you - copy pasting it will not magically make it work (somewhat of a common misconception when I post code samples).

Why do you need to check if the original parent exists since the original parent is workspace?

Is this just incase it was something in workspace?

That’s the game. I’m not making any assumptions about OP’s environment; it wasn’t provided and all I’m doing is supplying a code sample. Therefore, it’s better to assume that the parent can be anything rather than to assume truthy variable values.

I’m pretty much a beginner, so I didn’t know about delay, this solved my issue, big thanks!

1 Like