Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make this wait and come back?

Asked by
FiredDusk 1466 Moderation Voter
9 years ago

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)

2 answers

Log in to vote
2
Answered by 9 years ago
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.

Ad
Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
9 years ago

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)
0
It never gave me weapon FiredDusk 1466 — 9y
0
Make sure `ClassicSword:Clone().Parent = playerWhoClicked.Backpack` is in your script? Validark 1580 — 9y

Answer this question