I am making a tool that when the player clicks an object, it will print the name of that object, but it is printing multiple times, specifically the amount of times the tool was equipped.
this is a localscript
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local equipped = false
script.Parent.RequiresHandle = false
script.Parent.Equipped:Connect(function()
equipped = true
mouse.Button1Up:Connect(function()
if equipped == true then
if not mouse.Target then return end
if mouse.Target then
local target = mouse.Target
print(target)
end
end
end)
end)
script.Parent.Unequipped:Connect(function()
equipped = false
end)
The mouse button up is inside the tool.Equipped function, meaning that each time you equip the tool, you will create a new mouse button up function. To fix this, you can simply move the function outside of the tool.Equipped function.
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local equipped = false
script.Parent.RequiresHandle = false
script.Parent.Equipped:Connect(function()
equipped = true
end)
script.Parent.Unequipped:Connect(function()
equipped = false
end)
mouse.Button1Up:Connect(function()
if equipped == true then
if not mouse.Target then return end
if mouse.Target then
local target = mouse.Target
print(target)
end
end
end)