Problems with using mouse events in a server script via remote events

Hello. I’ve been having several problems with this radio system I’m making. I’ve provided the scripts and videos below.

First of all, when my mouse hovers over the volume trigger (ClickDetector), I can change the volume via scrolling just fine, but when my mouse stops hovering over the trigger, I’m still able to change the volume even if I’m not hovering over it.

Second, if I hover my mouse over the volume trigger several times and I happen to scroll, the volume and the orientation get changed a bigger amount as if the function was fired several times at the same time.

A server script inside the radio model:

VolumeTrigger.MouseHoverEnter:Connect(function(Player)
	local HintGui = Player.PlayerGui:FindFirstChild("Hint")

	HintGui.TextLabel.Text = "Increase/Decrease volume (Scroll)"
	HintGui.TextLabel.Visible = true

	if On == true then
		HoveringEvent:FireClient(Player)
		
		ScrollUp.OnServerEvent:Connect(function(Player)
			if Sound.Volume < 1 then
				VolumeKnob.CFrame = VolumeKnob.CFrame * CFrame.Angles(math.rad(5), 0, 0)
				Sound.Volume = Sound.Volume + 0.02
				print(VolumeKnob.Orientation.X)
			end
		end)
		
		ScrollDown.OnServerEvent:Connect(function(Player)
			if Sound.Volume > 0 then
				VolumeKnob.CFrame = VolumeKnob.CFrame * CFrame.Angles(math.rad(-5), 0, 0)
				Sound.Volume = Sound.Volume - 0.02
				print(VolumeKnob.Orientation.X)
			end
		end)
	end
end)

A client script in StarterCharacterScripts:

local HoveringEvent = game.ReplicatedStorage.HoveringEvent
local ScrollUp = game.ReplicatedStorage.ScrollUp
local ScrollDown = game.ReplicatedStorage.ScrollDown

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

HoveringEvent.OnClientEvent:Connect(function()
	Mouse.WheelForward:Connect(function()
		ScrollUp:FireServer()
	end)
	
	Mouse.WheelBackward:Connect(function()
		ScrollDown:FireServer()
	end)
end)

Any help would be appreciated.

2 Likes

Everytime the remote event fires, it will bind ANOTHER listener to Mouse.WheelForward and Mouse.WheelBackward

Your server script has the same issue, where on MouseHoverEnter, it will bind multiple listeners to ScrollUp.OnServerEvent.

You need to disconnect the events after you’re done listening.

Here’s what that could look like in your client script:

local wheelForwardConnection;
local wheelBackwardConnection;

HoveringEvent.OnClientEvent:Connect(function()
	if wheelForwardConnection then
		wheelForwardConnection:Disconnect()
	end
	if wheelBackwardConnection then
		wheelBackwardConnection:Disconnect()
	end
	wheelForwardConnection = Mouse.WheelForward:Connect(function()
		ScrollUp:FireServer()
	end)
	
	wheelBackwardConnection = Mouse.WheelBackward:Connect(function()
		ScrollDown:FireServer()
	end)
end)

Although, this should work there’s other ways to accomplish it, I’m just demonstrating how connections work.
You could also disconnect the events on MouseHoverLeave for example.

2 Likes

Thank you! I didn’t know you could disconnect the functions this way!

1 Like

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