Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to Delete only One Tool with the code given?

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

✓ Accepted answer

: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. 

2 comments

0

Thank you so much! You're right, I'm super terrible with code and you know exactly what you're talking about.

0

I like your wording, "No duh"

This discussion is preserved as it was. New answers, comments, and votes are closed.