Is there a way to disable scripts in a tool while the tool is in the players backpack?

Hey.

So i was wondering if there was anyway to disable a tools scripts. I couldn’t find a way so I found unequipping the tool from the players backpack and then destroying it was the most efficent. The problem is, even if you are touching the part, the tool gets destroyed for no reason and most of the time the tool does not work.

Here is the script:

local repStorage = game:GetService("ReplicatedStorage")
local tool = repStorage:WaitForChild("GK") 
local players = game:GetService("Players")
local part = workspace:WaitForChild("GoaliePartBlues")
local union = workspace:WaitForChild("Union")
local partPlayers = {}

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then
		local player = players:GetPlayerFromCharacter(hit.Parent)
		local character = hit.Parent
		local humanoid = character:WaitForChild("Humanoid")
		if player.TeamColor == BrickColor.new("Bright green") then
			if not table.find(partPlayers, player) then
				table.insert(partPlayers, player)
				local backpack = player:WaitForChild("Backpack")
				local tools = character:GetChildren()
				if not backpack:FindFirstChild("GK") and not character:FindFirstChild("GK") then
					local toolClone = tool:Clone()
					toolClone.Parent = player:WaitForChild("Backpack")
					for i, tool in pairs(backpack:GetChildren()) do
						if tool.Name == "GK" then
							humanoid:EquipTool(tool)
						end
					end
				end
			end
		end
	end
end)

part.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then
		local player = players:GetPlayerFromCharacter(hit.Parent)
		local character = hit.Parent
		if player.TeamColor == BrickColor.new("Bright green") then 
			for i, v in pairs(partPlayers) do
				if v.Name == player.Name then
					table.remove(partPlayers, i)
					local tools = character:GetChildren()
					for i, tool in pairs(player:WaitForChild("Backpack"):GetChildren()) do
						if tool.Name == "GK" then
							--do nothing for now
						end
					end
					for i, tool in pairs(tools) do
						if tool.Name == "GK" then
							--do nothing for now
						end
					end
				end
			end
		end
	end
end)


union.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then
		task.wait(.5)
		local player = players:GetPlayerFromCharacter(hit.Parent)
		local character = hit.Parent
		local humanoid = character:WaitForChild("Humanoid")
		local backpack = player:WaitForChild("Backpack")
		local tools = character:GetChildren()
		for i, tool in pairs(backpack:GetChildren()) do
			if tool.Name == "GK" then
				task.wait(0.5)
				
				tool:Destroy()
			end
		end
		for i, tool in pairs(tools) do
			if tool.Name == "GK" then
			
				task.wait(0.5)
				tool:Destroy()
			end
		end
	end
end)

Any help would be appreciated! Thanks.

umm yes it is i suppose you just need to make sure that the player didnt equiped the tool that you can do by

Humanoid:UnequipTools()
1 Like

I tried that before but it is really glitchy, especially when you are touching a part

i see then what you can do is use objectvalues

im sorry but, what are object values?

its ok ill explain what ObjectValues are

Explaination
ObjectValues are values that work similar to number values and string values. In an object value you can set the value to be a instance and object values are useful for tracking an objects directory for example
Lets say “X” is a part and its Directory Is “Z” when your using code you will be like

Z.X

but hold up what happenes if before that code “X” gets parented to “Y” the code wont work so a object value makes the code work by tracking it example

script.ObjectValue.Value = X
script.ObjectValue.Value.Parent = Z
print(script.ObjectValue.Value.Parent) -- Z
script.ObjectValue.Value.Parent = Y
print(script.ObjectValue.Value.Parent) -- Y

Make a check inside the touch events.

if ToolEnabled then
--run code
end

Connect Equipped and Unequipped events and use them to change the ToolEnabled variable to either false or true.

tool.Equipped:Connect(function()
ToolEnabled = true
end)

tool.Unequipped:Connect(function()
ToolEnabled = false
end)