Messaging Service running multiple times

For some reason whenever I use messaging service, the first time I call it, it runs the code once but when i call it for the second time it runs the code 2 times instead of running it once and it keeps on going in that pattern. Does anyone know how to stop this? Here’s my script:

game.ReplicatedStorage.SendMsg.OnServerEvent:Connect(function(player, Server)
	
	local messagingService = game:GetService("MessagingService")
	
	local number = 0

	messagingService:SubscribeAsync("Plane", function()
        print("test")
		number += 1
		if number == 1 then return end
	end)
	
	messagingService:PublishAsync("Plane")
		
end)
1 Like

messagingService is a service. That means it’s global. Every time you fire SendMsg, you connect the plane function. Then, you run the plane function. But the old plane function is also still connected, so when you publishAsync, you call all plane functions.

1 Like

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