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

How to Delete only One Tool with the code given?

Asked by 6 years ago

I want to delete just a Sword...

Thanks for helping out in advance.


function onTouched(hit) if hit.Parent:findFirstChild("Humanoid")~=nil then local user=game.Players:GetPlayerFromCharacter(hit.Parent) if user~=nil then local backpack=user:findFirstChild("Backpack") if backpack~=nil then local p=backpack:GetChildren() for i=1,#p do if p[i].className=="Sword" then p[i]:remove() end if p[i].className=="Tool" then p[i]:remove() end end end end end end script.Parent.Touched:connect(onTouched)

1 answer

Log in to vote
1
Answered by 6 years ago

:Destroy() not :remove(). BOY WAKE UP IT'S NOT 2010. :FindFirstChild() not :findFirstChild()

You shouldn't use too many "~= nil" in your scripts. No duh every player has a backpack. Let me clean up your messy deprecated code.

script.Parent.Touched:Connect(function(part)
    local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
    if plr then
        for _, child in pairs(plr.Backpack:GetChildren()) do
            if child.Name == "Sword" then
                child:Destroy()
            end
        end
    end
end)

-- clean. 
0
Thank you so much! You're right, I'm super terrible with code and you know exactly what you're talking about. robloxquestionnaire1 30 — 6y
0
I like your wording, "No duh" Internal_1 344 — 6y
Ad

Answer this question