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

Removing tools from players character not working? ( Advance )

Asked by
14dark14 167
4 years ago

When a player enters this area he gets a sword, and when he leaves it removed the sword from players Backpack, but not the player's character, HELP!

local ToolNames = {"Sword"}

local part = script.Parent
local Debounces = {}
local Storage = game:GetService("ServerStorage")

function RemoveWeapons(parent)
    local weapons = parent:GetChildren()
    for i = 1,#weapons do
        for _, v in pairs(ToolNames) do 
            if (weapons[i].className == "Tool") and (weapons[i].name == v) then
                  weapons[i]:Destroy()
              end
          end
     end
end

local function onTouch(part)
     Player = game.Players:GetPlayerFromCharacter(part.Parent)
        if Player and not Debounces[Player.UserId] then
            Debounces[Player.UserId] = true
Backpack = Player:WaitForChild("Backpack")
        for i = 1, #ToolNames do
            local Tool = Storage:FindFirstChild(ToolNames[i])
            if Tool then
            Tool:clone().Parent = Backpack

            end
        end
end
end

local function onTouchEnded(part)
   if Player and Debounces[Player.UserId] then  
    if (table.maxn(Backpack:GetChildren()) > 0) then
    RemoveWeapons(Backpack)

RemoveWeapons(Player.Character)  <----- Error Error here
   Debounces[Player.UserId] = false  
end
end
end

part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)

2 answers

Log in to vote
1
Answered by 4 years ago

On line 32, you are checking if the player's backpack has more than 0 children.

if (table.maxn(Backpack:GetChildren()) > 0) then

Since the item would be parented to the Character if the player is holding an item, the backpack would have 0 children, therefore it wouldn't run.

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
script.Parent.Touched:connect(function(hit)
    local Humanoid = hit.Parent:FindFirstChild("Humanoid")
    if Humanoid then
        for i,item in pairs(hit.Parent:GetChildren()) do
            if item.Name == "Sword" and item:IsA("Tool") then
                item:Destroy()
            end
        end
        for i,item in pairs(game.Players[hit.Parent.Name].Backpack:GetChildren()) do
            if item.Name == "Sword" and item:IsA("Tool") then
                item:Destroy()
            end
        end
    end
end)

Make sure to put this inside of the part that you want the tool to be destroyed in.

If this is the solution make this your answered question.

0
What is this? did you even read my script? or just answer based on the title 14dark14 167 — 4y

Answer this question