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)
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)
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)
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)
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)
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)