RemoteEvent gets fired the number of time my tool gets used

Hello! As the title says, the RemoteEvent I use to build a block gets fired the number of time my tool gets used.
I have no idea why, could someone please help me?

Here is my code that fires the RemoteEvent:

(NOTE: The BlockSelectionne BindableEvent fires when the player clicks on a block on a GUI. It’s not problematic because I used print() on both scripts and it only fires once.)

-- Services
UserInputService = game:GetService("UserInputService")

-- Références
local LocalPlayer = game.Players.LocalPlayer
local BuildGui = LocalPlayer:WaitForChild("PlayerGui", 60):WaitForChild("BuildGui", 60)
local Hammer = script.Parent
local mouse = LocalPlayer:GetMouse()

-- Remotes & Bindables
local BlockSelectionne = game.ReplicatedStorage.BlockSelectionne
local ConstruireBlock = game.ReplicatedStorage.ConstruireBlock

-- Faire apparaître le GUI quand le joueur équipe le marteau
Hammer.Equipped:Connect(function()
	BuildGui.Enabled = true
end)

-- Faire disparaître le GUI quand le joueur déséquipe le marteau
Hammer.Unequipped:Connect(function()
	BuildGui.Enabled = false
end)

-- Déclarer la variable Block qui pointe vers le block séléctionné et obtenir où la souris a clické dans le monde.
Hammer.Activated:Connect(function()
	BlockSelectionne.Event:Connect(function(blockName)
		mouse.Button1Down:Connect(function()
			print("placé ", blockName)
			ConstruireBlock:FireServer(tostring(blockName), mouse.Hit, mouse.Target)
		end)
	end)
end)


And here is my code that executes when it is fired:

-- Remotes
local ConstruireBlock = game.ReplicatedStorage.ConstruireBlock

ConstruireBlock.OnServerEvent:Connect(function(player, blockName, mouseHit, mouseTarget)
	local newBlock = game.ServerStorage.Blocks[blockName]:Clone()
	print(newBlock)
	newBlock.CFrame = mouseHit
	newBlock.Parent = game.Workspace
end)

Thank you for reading.

1 Like

You’re likely experiencing this problem because every time you activate your tool, you’re creating a new connection.

To maintain a single connection for the BlockSelectionne event without creating it every time the tool is activated, you can move the event connection outside of the Hammer.Activated event.

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

-- References
local LocalPlayer = game.Players.LocalPlayer
local BuildGui = LocalPlayer:WaitForChild("PlayerGui", 60):WaitForChild("BuildGui", 60)
local Hammer = script.Parent
local mouse = LocalPlayer:GetMouse()

-- Remotes & Bindables
local BlockSelectionne = game.ReplicatedStorage.BlockSelectionne
local ConstruireBlock = game.ReplicatedStorage.ConstruireBlock

-- Connection variable for the BlockSelectionne event
local blockSelectionneConnection

-- Faire apparaître le GUI quand le joueur équipe le marteau
Hammer.Equipped:Connect(function()
	BuildGui.Enabled = true
end)

-- Faire disparaître le GUI quand le joueur déséquipe le marteau
Hammer.Unequipped:Connect(function()
	BuildGui.Enabled = false
end)

-- Déclarer la variable Block qui pointe vers le block sélectionné et obtenir où la souris a cliqué dans le monde.

-- Connect the BlockSelectionne event outside of Hammer.Activated
blockSelectionneConnection = BlockSelectionne.Event:Connect(function(blockName)
	mouse.Button1Down:Connect(function()
		print("placé ", blockName)
		ConstruireBlock:FireServer(tostring(blockName), mouse.Hit, mouse.Target)
	end)
end)

-- Disconnect the BlockSelectionne event when the tool is unequipped
Hammer.Unequipped:Connect(function()
	if blockSelectionneConnection then
		blockSelectionneConnection:Disconnect()
	end
end)

In this modified version, the blockSelectionneConnection is established outside of the Hammer.Activated event. This ensures that the connection is made only once. Additionally, I’ve added a disconnection of the event in the Hammer.Unequipped event to clean up the connection when the tool is unequipped, preventing any potential memory leaks.

2 Likes

Woah fast reply.
Thank you, going to try this now and let you know if it worked by editing this.
EDIT: It works sorry I couldn’t get back.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.