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:
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.