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

Removing part on touch?

Asked by
FiredDusk 1466 Moderation Voter
8 years ago

This gives me 1 point when it is touched. When it is touched, how would I make it removed?

amnt = 1
function onTouched(part)
    local h = part.Parent:findFirstChild("Humanoid")
    if (h~=nil) then
        local thisplr = game.Players:findFirstChild(h.Parent.Name)
        if (thisplr~=nil) then
            local stats = thisplr:findFirstChild("leaderstats")
            if (stats~=nil) then
                local score = stats:findFirstChild("Points")
                if (score~=nil) then
                    score.Value = score.Value + amnt

                end
            end
        end

    end
end

script.Parent.Touched:connect(onTouched)
script.Parent.Touched:Remove()

1 answer

Log in to vote
1
Answered by
dyler3 1510 Moderation Voter
8 years ago

You simply need to use the :Destroy() method. It removes the part exactly how you want.

We'll use it like this:

game.Workspace.Part:Destroy() --Destroys the part

Now let's apply it to your script:

amnt = 1
function onTouched(part)
    local h = part.Parent:findFirstChild("Humanoid")
    if (h~=nil) then
        local thisplr = game.Players:findFirstChild(h.Parent.Name)
        if (thisplr~=nil) then
            local stats = thisplr:findFirstChild("leaderstats")
            if (stats~=nil) then
                local score = stats:findFirstChild("Points")
                if (score~=nil) then
                    score.Value = score.Value + amnt
                    script.Parent:Destroy() --Added it in right here to destroy part
                end
            end
        end
    end
end

script.Parent.Touched:connect(onTouched)

It should work now.


Anyways, hope this helped. If you have any further problems/questions, please leave a comment below :P

-Dyler3

1
Thanks! FiredDusk 1466 — 8y
0
No prob, glad I could help :p dyler3 1510 — 8y
Ad

Answer this question