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

Equipping tool breaks my script ( Advance help needed ) ?

Asked by
14dark14 167
4 years ago
Edited 4 years ago

Please read Ok, I've been struggling for past 2 days now to make this work, I'm almost there I got this problem yesterday and I still haven't figured out how to fix it. I made this script where if you walk on a platform you get a sword and if you leave the sword disappears. it works fine, but when I equip the tool the script detects that I have stopped touching the part and destroys the sword. I'm using Touched and TouchEnded.

Script inside a part in workspace

local ToolNames = {"Sword"}
local Storage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local part = script.Parent
local debounce = false
local tofastman = true

function RemoveWeapons(parent)
    local weapons = parent:GetChildren()
    for i = 1,#weapons do
            if (weapons[i].className == "Tool") then
                  weapons[i]:Destroy()
          end
     end
end

local function onTouched(part)
     Player = Players:GetPlayerFromCharacter(part.Parent)
         if Player and Player.Character then
         if (part == part.Parent["Left Leg"]) then
         if debounce == false then
         debounce = true
            Backpack = Player:WaitForChild("Backpack")
            for i = 1, #ToolNames do
            local Tool = Storage:FindFirstChild(ToolNames[i])
            if Tool then
            Tool:clone().Parent = Backpack
            tofastman = false
            end
        end
        end
        end
    end
end

local function onLeft(part)
    if tofastman == false then
          Player.Character.Humanoid:UnequipTools()
           RemoveWeapons(Backpack)
           debounce = false
      tofastman = true
    end
end

part.Touched:Connect(onTouched)
part.TouchEnded:Connect(onLeft)
0
Depending upon your setup I would go for a distance check, position check or ray based solution. Using the touched evetn for this is not going to yield the best results. User#5423 17 — 4y

Answer this question