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

how to re add an object after "remove"?

Asked by
nicros 165
8 years ago

so ive got this script'

script.Parent.Touched:connect(function()
    for _,Player in pairs(game.Players:GetPlayers()) do
        if Player:FindFirstChild("leaderstats") then
            Player.leaderstats.Wins.Value = Player.leaderstats.Wins.Value +1

        end
    end
    script.Parent:remove()
    --how do i add the object right here?
end)

you hit the brick it adds 1 to Wins and it removes it so it doesnt add more to win s as u walk through

how do i re add the brick ?

0
first you should clone it docrobloxman52 407 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

2 Ways, either add a debounce or remove and bring back the part (Remove does not fully get rid of the object. The object still exists when you use remove).


Debounce

A debounce is a way to stop a script from going for a moment and then letting it run after a certain amount of time. Look at this example from the Wiki.

Workspace.Button.Touched:connect(function(hit)
    print("Button pressed") --Print the message
    wait(1)                 --Wait for 1 second
    print("Hi :D")          --Print another message
end)

Output you expect:

Button pressed Hi :D

Actual Output:

Button pressed Button pressed Button pressed Button pressed Button pressed Hi :D Hi :D Hi :D Hi :D Hi :D

Fixed code:

local debounce = false
--Store whether the button is pressed in a local variable

Workspace.Button.Touched:connect(function(hit)
    if not debounce then
    -- Is it not pressed?

        debounce = true
        -- Mark it as pressed, so that other handlers don't execute

        print("Button pressed")
        wait(1)
        print("Hi :D")
        -- Do Stuff

        debounce = false
        -- Mark it as not pressed, so other handlers can execute again
    end
end)

Output:

Button pressed Hi :D


Remove

Remove is a way to get rid of objects, but the problem is it doesn't actually remove the parts. It just sets the parent to nil, meaning it can be returned. This is why I use Destroy instead. Anyway, using the knowledge that remove doesn't completely remove the part, we can exploit this in our favor.

local part = workspace.Part
part:remove()--Same thing as: part.Parent = nil
wait(3)
part.Parent = workspace--part gets removed and then gets brought back after 3 seconds

Still, use Destroy instead for your primary way of getting rid of objects.



Final Product

These are the different ways of doing this:

Debounce

local debounce = false
script.Parent.Touched:connect(function()
if not debounce then
    debounce = true
    for _,Player in pairs(game.Players:GetPlayers()) do
        if Player:FindFirstChild("leaderstats") then
            Player.leaderstats.Wins.Value = Player.leaderstats.Wins.Value +1
        wait(5)
        end
    end
    debounce = false
end
end)

Debounce+Invisible part

local debounce = false
script.Parent.Touched:connect(function()
if not debounce then
    debounce = true
    script.Parent.Transparency = 1
    script.Parent.CanCollide = false
    for _,Player in pairs(game.Players:GetPlayers()) do
        if Player:FindFirstChild("leaderstats") then
            Player.leaderstats.Wins.Value = Player.leaderstats.Wins.Value +1
        wait(5)
        end
    end
    debounce = false
    script.Parent.Transparency = 0
    script.Parent.CanCollide = true
end
end)

Remove/Parent-To-nil

local part = script.Parent
local parent = part.Parent
part.Touched:connect(function()
    for _,Player in pairs(game.Players:GetPlayers()) do
        if Player:FindFirstChild("leaderstats") then
            Player.leaderstats.Wins.Value = Player.leaderstats.Wins.Value +1

        end
    end
    part:remove()
    wait(3) --change to how many seconds you want it to take.
    part.Parent = parent
end)


Hope it helps!

0
thanks for the very very very detailed answered, will help me get through a lot of problems later on im sure nicros 165 — 8y
Ad

Answer this question