Issue with tool.Equipped

Hello, here is my issue. So the issue is that, I want the function to run only when the tool is equipped. I tried and it doesn’t work. Thanks in advance if you help me fix it.

-- Variables
local plr = game:GetService("Players").LocalPlayer
local plrGui = plr.PlayerGui
local cuffGui = plrGui:WaitForChild("CuffGUI") 
local players = game:GetService("Players")
local Owner = 289079558
local Mouse = plr:GetMouse()
local CurrentTarget = nil
local ItemsFrame = plr.PlayerGui:WaitForChild("CuffGUI"):WaitForChild("ItemsFrame")
local RedactedTeam = game:GetService("Teams").RedTeam
local TextButton = game:GetService("ReplicatedStorage"):WaitForChild("ToolButton")
local maxDistance = 5

-- Functions

local function MouseDon()
	Mouse.Move:Connect(function()
		if Mouse.Target then
			if plr:DistanceFromCharacter(Mouse.Target.Position) < maxDistance then
				local tPlayer = game:GetService("Players"):GetPlayerFromCharacter(Mouse.Target.Parent)
				if tPlayer then
					if tPlayer.Team ~= RedactedTeam then
						
						CurrentTarget = tPlayer.Backpack:GetChildren()
						
						for i, v in pairs(CurrentTarget) do
							local frame = TextButton:Clone()
							TextButton.Parent = ItemsFrame
							TextButton.Text = v.Name
						end
					end
				end
			end
		end
	end)
end

script.Parent.Equipped:Connect(MouseDon)

players.PlayerAdded:Connect(function()
	if plr.UserId == 289079558 then
		print("")
	else
		script:Destroy()
	end
end)

Try:

local equipped = false
--remove local function
Mouse.Move:Connect(function()
		if Mouse.Target and equipped then
			if plr:DistanceFromCharacter(Mouse.Target.Position) < maxDistance then
				local tPlayer = game:GetService("Players"):GetPlayerFromCharacter(Mouse.Target.Parent)
				if tPlayer then
					if tPlayer.Team ~= RedactedTeam then
						
						CurrentTarget = tPlayer.Backpack:GetChildren()
						
						for i, v in pairs(CurrentTarget) do
							local frame = TextButton:Clone()
							TextButton.Parent = ItemsFrame
							TextButton.Text = v.Name
						end
					end
				end
			end
		end
	end)

script.Parent.Equipped:Connect(function()
   equipped = true
end)
script.Parent.Unequipped:Connect(function()
   equipped = false
end)

Nope. It doesn’t work. Hmmmmmm.

does it loop? if yes then try this:

local equipped = false
--remove local function
game:GetService("RunService").Stepped:Connect(function()
		if Mouse.Target and equipped then
			if plr:DistanceFromCharacter(Mouse.Target.Position) < maxDistance then
				local tPlayer = game:GetService("Players"):GetPlayerFromCharacter(Mouse.Target.Parent)
				if tPlayer then
					if tPlayer.Team ~= RedactedTeam then
						
						CurrentTarget = tPlayer.Backpack:GetChildren()
						
						for i, v in pairs(CurrentTarget) do
							local frame = TextButton:Clone()
							TextButton.Parent = ItemsFrame
							TextButton.Text = v.Name
						end
					end
				end
			end
		end
	end)

script.Parent.Equipped:Connect(function()
   equipped = true
end)
script.Parent.Unequipped:Connect(function()
   equipped = false
end)
1 Like

Nope, still doesn’t work.

It doesn’t work if the tool must be equipped.

Equipped and unequipped may cause tons of issues i recommend looking for Instance.AncestryChanged

p.AncestryChanged:Connect(function(child, parent)
    if parent == player.Backpack then
         equipped = false
    elseif parent == player.Character then
         equipped = true
    end
end)

Try adding prints in ‘Equipped’ and ‘Unequipped’, see if the events even trigger.

Tested it and it doesn’t print the events.

1 Like

Could you send screenshots of both the properties and hierarchy (in Explorer) of your tool?

What do you mean by “hierarchy”? I am new to this expression, sorry.
Capture d’écran (13)

1 Like

Maybe try to disconnect mouse move event, when a tool isn’t equipped?

-- Variables
local plr = game:GetService("Players").LocalPlayer
local plrGui = plr.PlayerGui
local cuffGui = plrGui:WaitForChild("CuffGUI") 
local players = game:GetService("Players")
local Owner = 289079558
local Mouse = plr:GetMouse()
local CurrentTarget = nil
local ItemsFrame = plr.PlayerGui:WaitForChild("CuffGUI"):WaitForChild("ItemsFrame")
local RedactedTeam = game:GetService("Teams").RedTeam
local TextButton = game:GetService("ReplicatedStorage"):WaitForChild("ToolButton")
local maxDistance = 5

local move 

-- Functions

local function MouseDon()
	move = Mouse.Move:Connect(function()
		if Mouse.Target then
			if plr:DistanceFromCharacter(Mouse.Target.Position) < maxDistance then
				local tPlayer = game:GetService("Players"):GetPlayerFromCharacter(Mouse.Target.Parent)
				if tPlayer then
					if tPlayer.Team ~= RedactedTeam then

						CurrentTarget = tPlayer.Backpack:GetChildren()

						for i, v in pairs(CurrentTarget) do
							local frame = TextButton:Clone()
							TextButton.Parent = ItemsFrame
							TextButton.Text = v.Name
						end
					end
				end
			end
		end
	end)
end

local function unEquipped()
	move:Disconnect()
end

script.Parent.Equipped:Connect(MouseDon)
script.Parent.Unequipped:Connect(unEquipped)

players.PlayerAdded:Connect(function()
	if plr.UserId == 289079558 then
		print("")
	else
		script:Destroy()
	end
end)

Try disabling ‘RequiresHandle’ on your tool.

Reminder Equipped will not fire if Tool.RequiresHandle and the tool, does not, in fact contain a BasePart called “Handle”

I added a handle to the tool, and it doesn’t fire Equipped/Unequipped

Ok I successfully fixed it.

For those who have the same issue, here the full script. I fixed it by disabling the script when it’s unequipped, and enabling it when it’s equipped.

-- Variables
local plr = game:GetService("Players").LocalPlayer
local plrGui = plr.PlayerGui
local cuffGui = plrGui:WaitForChild("CuffGUI") 
local players = game:GetService("Players")
local Owner = 289079558
local Mouse = plr:GetMouse()
local CurrentTarget = nil
local ItemsFrame = plr.PlayerGui:WaitForChild("CuffGUI"):WaitForChild("ItemsFrame")
local RedactedTeam = game:GetService("Teams").fzfzazf
local TextButton = game:GetService("ReplicatedStorage"):WaitForChild("ToolButton")
local maxDistance = 5

-- Functions

script.Parent.Equipped:Connect(function()
	Mouse.Move:Connect(function()
         Mouse.Button1Down:Connect(function()
		 if Mouse.Target then
		       	if plr:DistanceFromCharacter(Mouse.Target.Position) < maxDistance then
				  local tPlayer = game:GetService("Players"):GetPlayerFromCharacter(Mouse.Target.Parent)
				  if tPlayer then
					    if tPlayer.Team ~= RedactedTeam then

						  CurrentTarget = tPlayer.Backpack:GetChildren()

						     for i, v in pairs(CurrentTarget) do
							     local frame = TextButton:Clone()
							     TextButton.Parent = ItemsFrame
							     TextButton.Text = v.Name
                            end
						end
					end
				end
			end
		end)
	end)
end)

script.Parent.Unequipped:Connect(function()
	script.Disabled = true
end)