Remote event function firing multiple times

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to fix the bug so that my remote event works as intended

  2. What is the issue? my remote event fires the more it is used, going up by 2 each time

  3. What solutions have you tried so far? setting the connection to nil, testing print, putting script inside coroutine wrap

Did you look for solutions on the Developer Hub? yes

this is my script here:


local module = {}

local Invoke = Instance.new("RemoteEvent")
Invoke.Name = "RockEvent"
local Modules = script.Packages
local partCacheMod = require(Modules.PartCache)
local cacheFolder
if not workspace:FindFirstChild("Debris") then
	cacheFolder = Instance.new("Folder")
	cacheFolder.Name = "Debris"
	cacheFolder.Parent = workspace
else
	cacheFolder = workspace.Debris
end

local partCache = partCacheMod.new(Instance.new("Part"), 1000, cacheFolder)

module.Rocks = function(Origin : "Part or Vector3",rockNumber : number,CanCollide : boolean,Size : Vector3,MaxLaunch : number,Time : number)
	local wrapper = coroutine.create(function()
		for _,v in pairs(workspace.Players:GetChildren()) do
			local mag = (Origin - v.HumanoidRootPart.Position).Magnitude
			if mag <= 50 then
				local safe,fail = pcall(function()
					local plrt = game.Players:GetPlayerFromCharacter(v)
					if plrt then
						Invoke:FireClient(plrt,Origin,rockNumber,CanCollide,Size,MaxLaunch,Time)
						game.ReplicatedStorage.CameraRemote:FireClient(plrt,"bump",nil,1)
					end
				end)
				if fail then
					warn(fail)
				end
			end
		end
	end)
	coroutine.resume(wrapper)
	connection = Invoke.OnClientEvent:Connect(function(Origin,rockNumber,CanCollide,Size,MaxLaunch,Time)
		local s,f = pcall(function()
			game.ReplicatedStorage.Ki.Position = Origin
		end)
		if f then
			Origin = Origin.Position
		end
	if Time == nil then
		Time = 2
	end
	for i = 1, rockNumber do
		local Part = partCache:GetPart()
		Part.Anchored = false
		Part.Name = "Part"
		Part.Shape = "Block"
		Part.Size = Size
		Part.CanCollide = false
		local a1 = Instance.new("Attachment")
		local a2 = Instance.new("Attachment")
		a1.Position = Vector3.new(0,1,0)
		a1.Parent = Part
		a2.Position = Vector3.new(0,-1,0)
		a2.Parent = Part
		local trail = script.Trail:Clone()
		trail.Parent = Part
		trail.Attachment0 = a1
		trail.Attachment1 = a2
		game.Debris:AddItem(trail,Time)
		game.Debris:AddItem(a1,Time)
		game.Debris:AddItem(a2,Time)
		local rayOrigin
		local s,f = pcall(function()
		rayOrigin = Origin
		Part.Position = Origin
		end)
		if f then
			rayOrigin = Origin.Position
			Part.Position = Origin.Position
		end
		local rayDirection = Vector3.new(0, -20, 0)
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {workspace.Players, workspace.FakeCharacters, workspace.Debris}
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		raycastParams.IgnoreWater = true
		local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
		if raycastResult then
		if raycastResult.Instance then
				Part.Color = raycastResult.Instance.Color
				Part.Material = raycastResult.Instance.Material
		elseif not raycastResult.Instance then
				return
			end
		elseif not raycastResult then
			return
		end
		Part.Velocity = Vector3.new(math.random(-100,MaxLaunch),math.random(0,MaxLaunch),math.random(-100,MaxLaunch))
		Part.CFrame = Part.CFrame * CFrame.Angles(math.rad(math.random(30, 50)), math.rad(math.random(30, 50)), math.rad(math.random(30, 50)))
		
		Part.Parent = workspace.Debris
		local wrap = coroutine.create(function()
			task.wait(Time)
			Part.CanCollide = CanCollide
			local tabel = {
				["Size"] = Vector3.new(0,0,0),
			}
			local ts = game:GetService("TweenService")
			local info = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.In)
			local tween = ts:Create(Part,info,tabel):Play()
			task.delay(2, function()
				partCache:ReturnPart(Part)
			end)
		end)
		coroutine.resume(wrap)
	end
	local wra = coroutine.create(function()
	task.wait(Time)
	connection = nil
	end)
	coroutine.resume(wra)
	end)
end

return module

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Setting the connection to nil does not disconnect the function. You need to use :Disconnect() in order to stop listening for events.

That is the first thing I tried, didn’t work either

ended up fixing it myself by changing :Connect to :Once

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