Decrease and increase value multiplying by 2 every time i use a tool

what is causing this issue?
it multiplies decrease and increase value after i use leash remover
i spent really long time trying to find a solution but i still didn’t

Leash server:

 script.Parent:WaitForChild("RemoteEvent")
script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, target)
	local ropeconstraint = Instance.new("RopeConstraint")
	local attachment0 = Instance.new("Attachment")
	local attachment1 = Instance.new("Attachment")
	local Distance = (player.Character.HumanoidRootPart.Position - target.Position).Magnitude
	local maxDistance = 50
    local minimumLength = 5.5
	local decrease = 0.4
	local increase = 0.4
	local blocks = target.Parent
	
	if blocks.Name == player.Name and (player.Character.HumanoidRootPart.Position - target.Position).Magnitude < maxDistance then
		attachment0.Parent = target
		attachment1.Parent = script.Parent.Parent.Torso
		ropeconstraint.Attachment0 = attachment0
		ropeconstraint.Attachment1 = attachment1
		ropeconstraint.Color = BrickColor.new("Black")
		ropeconstraint.Visible = true
		ropeconstraint.Parent = script.Parent.Parent.Torso
		ropeconstraint.Length = Distance
		script.Parent.RemoteEvent2.OnServerEvent:Connect(function()
			ropeconstraint.Length += increase
print(increase)
		end)
		script.Parent.RemoteEvent3.OnServerEvent:Connect(function()
			player.Character.Torso.Anchored = true
			ropeconstraint.Length -= decrease
			print(decrease)
			wait(0.01)
			player.Character.Torso.Anchored = false
end)
while wait(0.01) do
			if ropeconstraint.Length < minimumLength then
				ropeconstraint.Length = minimumLength
			decrease = 0
			end
end
		while wait(0.01) do
if ropeconstraint.Length > minimumLength then
				decrease = 0.4
			end do
			end
		end
	end
	end)

Leash Client:

local keyPressed = false
local keyPressed2 = false
script.Parent.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		script.Parent.RemoteEvent:FireServer(mouse.Target, mouse.TargetSurface)
	end)
	local player = game.Players.LocalPlayer
	local UserInputService = game:GetService("UserInputService")
	UserInputService.InputBegan:Connect(function(inputObject, gameProcessedEvent)
		if not gameProcessedEvent then
			if inputObject.KeyCode == Enum.KeyCode.E then
				print("E")
				script.Parent.RemoteEvent2:FireServer()
			end
		end
	end)
	UserInputService.InputBegan:Connect(function(inputObject2, gameProcessedEvent2)
		if not gameProcessedEvent2 then
			if inputObject2.KeyCode == Enum.KeyCode.Q then
				print("Q")
				script.Parent.RemoteEvent3:FireServer()
			end
		end
	end)
end)

[Screen Capture 14.07.2023, 13.39.18]

1 Like

I had to rename your variables and instances, “remoteevent1” is darn near unreadable. Your issue was due to not disconnecting listeners, so each time you equip a tool, it connects e.g. Button1Down. So each time you click, it fires the RE once for every time you’ve equipped the tool. To fix it, clean up connections once they’re no longer needed.

Try this:

--Client
local UserInputService = game:GetService("UserInputService")

local tool = script.Parent
local cs = {} --Connections to be cleaned when tool is unequipped

function push<T>(t: {T}, item: T): T
	table.insert(t, item)
	return item
end

function clean(item)
	if typeof(item) == "Instance" then
		item:Destroy()
	elseif typeof(item) == "RBXScriptConnection" then
		item:Disconnect()
	end
end

function cleanAll()
	for _, c in cs do clean(c) end
end

tool.Equipped:Connect(function(mouse)
	tool.Unequipped:Once(cleanAll)

	push(cs, mouse.Button1Down:Connect(function() --Gets cleaned when tool is unequipped
		tool.TargetClickedRemoteEvent:FireServer(mouse.Target)
	end))

	push(cs, UserInputService.InputBegan:Connect(function(input, gameProcessedEvent) --Gets cleaned when tool is unequipped
		if gameProcessedEvent then return end

		if input.KeyCode == Enum.KeyCode.E then
			tool.LengthenRopeRemoteEvent:FireServer()
		elseif input.KeyCode == Enum.KeyCode.Q then
			tool.ShortenRopeRemoteEvent:FireServer()
		end
	end))
end)
--Server
local RunService = game:GetService("RunService")

local tool = script.Parent
tool:WaitForChild("TargetClickedRemoteEvent")

local connectRange = 50
local minimumLength = 5.5
local maximumLength = 10
local decrease = 0.4
local increase = 0.4

local cs = {} --Connections and instances to be cleaned when a leash is disconnected
local connectedBlock

function push<T>(t: {T}, item: T): T
	table.insert(t, item)
	return item
end

function clean(item)
	if typeof(item) == "Instance" then
		item:Destroy()
	elseif typeof(item) == "RBXScriptConnection" then
		item:Disconnect()
	end
end

function cleanAll()
	for _, c in cs do clean(c) end
end

function lengthenLeash(leash)
	leash.Length = math.clamp(leash.Length + increase, minimumLength, maximumLength)
	print(leash.Length)
end

function shortenLeash(leash)
	character.Torso.Anchored = true
	leash.Length = math.clamp(leash.Length - decrease, minimumLength, maximumLength)
	print(leash.Length)
	RunService.Heartbeat:Wait()
	character.Torso.Anchored = false
end

function connectLeash(character, block)
	local distance = (character.PrimaryPart.Position - block.Position).Magnitude

	local attachment0 = push(cs, Instance.new("Attachment")) --Gets cleaned when you call cleanAll, e.g. when disconnecting a "leash"
	attachment0.Parent = block

	local attachment1 = push(cs, Instance.new("Attachment")) --Gets cleaned when you call cleanAll
	attachment1.Parent = tool.Parent.Torso

	local leash = push(cs, Instance.new("RopeConstraint")) --Gets cleaned when you call cleanAll
	leash.Attachment0 = attachment0
	leash.Attachment1 = attachment1
	leash.Color = BrickColor.new("Black")
	leash.Visible = true
	leash.Length = distance
	leash.Parent = character.Torso

	connectedBlock = block

	push(cs, tool.LengthenRopeRemoteEvent.OnServerEvent:Connect(function()  --Gets cleaned when you call cleanAll
		lengthenLeash(leash)
	end))

	push(cs, tool.ShortenRopeRemoteEvent.OnServerEvent:Connect(function()  --Gets cleaned when you call cleanAll
		shortenLeash(leash)
	end))

	return leash
end

tool.TargetClickedRemoteEvent.OnServerEvent:Connect(function(player, target)
	if connectedBlock ~= nil then return end

	local blocks = target.Parent
	if blocks.Name ~= player.Name then return end
	
	local character = tool.Parent
	local distance = (character.HumanoidRootPart.Position - target.Position).Magnitude
	if distance > connectRange then return end

	local leash = connectLeash(character, target)
end)