When I click a part using click detector, it gets removed. I want it to get removed then wait 20 seconds to come back.
ClassicSword = game.Workspace.ClassicSword ClassicSword2 = game.Lighting.ClassicSword function onClicked(playerWhoClicked) ClassicSword:Destroy() --Use Destroy, it is MUCH more efficient. wait(.5) ClassicSword2:Clone().Parent = playerWhoClicked.Backpack --Parent the tool from playerWhoClicked, which is the character who clicked the ClickDetector which is on line 5. end script.Parent.ClickDetector.MouseClick:connect(onClicked)
ClassicSword = game.Workspace.ClassicSword ClassicSword2 = game.Lighting.ClassicSword function onClicked(playerWhoClicked) spawn(function() wait(20) ClassicSword2:Clone().Parent = playerWhoClicked.Backpack end) ClassicSword:Destroy() end script.Parent.ClickDetector.MouseClick:connect(onClicked)
Using the spawn function, I can create a script in a script basically. The script runs separate from the main one, so waits aren't accounted for. in the main one.
Hungryjaffer technically answered your question, but didn't really explain what's going on.
To make something wait 20 seconds, we can use wait(20)
To copy an object we are going to use the clone()
function. Note that when you clone things you don't have to parent it to the Workspace immediately.
ClassicSword = game.Workspace.ClassicSword --We only need the one in the workspace now function onClicked(playerWhoClicked) -- Remove ClassicSword from the game -- Wait 20 seconds -- Put it back local backup = ClassicSword:Clone() --Save a copy of ClassicSword ClassicSword:Destroy() --Remove ClassicSword from the game wait(20) backup.Parent = workspace --Put it back into the game end script.Parent.ClickDetector.MouseClick:connect(onClicked)
Also, if you want to give the player the tool they just clicked on, add the following line into your script:
ClassicSword:Clone().Parent = playerWhoClicked.Backpack
ClassicSword = game.Workspace.ClassicSword --We only need the one in the workspace now function onClicked(playerWhoClicked) -- Remove ClassicSword from the game -- Wait 20 seconds -- Put it back local backup = ClassicSword:Clone() --Save a copy of ClassicSword ClassicSword:Clone().Parent = playerWhoClicked.Backpack -- Give the Sword to the player who clicked ClassicSword:Destroy() --Remove ClassicSword from the game wait(20) backup.Parent = workspace --Put it back into the game end script.Parent.ClickDetector.MouseClick:connect(onClicked)