This feels like it should be so easy but I can't figure it out. When the player picks up a new tool, I want their currently equipped tool to be destroyed. I tried this:
01 | function Equipped(Mouse) |
02 | Handle.Anchored = false |
03 | Character = Tool.Parent |
04 | Humanoid = Character:FindFirstChild( "Humanoid" ) |
05 | Torso = Character:FindFirstChild( "HumanoidRootPart" ) |
06 | Player = Players:GetPlayerFromCharacter(Character) |
07 |
08 | Character:FindFirstChildOfClass( "Tool" ):Destroy() |
09 | --This line is the important one^^^ |
10 | end |
but it tries to destroy the tool that is being equipped - the parent of the script. I could probably do a longer system with ChildAdded and classname == "Tool" but I feel like this should be an easy thing to do. Any thoughts?
01 | function Equipped(Mouse) |
02 | Handle.Anchored = false |
03 | local Character = Tool.Parent |
04 | local Children = Character:GetChildren() |
05 | local Humanoid = Character:FindFirstChild( "Humanoid" ) |
06 | local Torso = Character:FindFirstChild( "HumanoidRootPart" ) |
07 | local Player = Players:GetPlayerFromCharacter(Character) |
08 |
09 | for i,v in paris(Children) do |
10 | If v.ClassName = = "Tool" then |
11 | If v.Name ~ = "Tool.Name" then |
12 | v:Destroy() |
13 | end |
14 | end |
15 | end |
16 | end |
What this does is Looks for all tools located in the character and deletes any that have a different name than the one equipped.
It's as simple as logging the last equipped tool, for example
local previoustool
tool.Equipped:connect(function() if previoustool then previoustool:Remove() end previoustool=tool end)