Mouse continuing to change CFrame despite LocalScript being deleted/disabled

:wave: Hello,

I made a camera parallax script for the main menu of my game. It works fine and updates each time the mouse is moved.
The loading screen is handled by a LocalScript which also controls the camera. Here’s the code:

Mouse.Move:Connect(function()
	Camera.CFrame = CFrame.new(Mouse.UnitRay.Direction.X, Mouse.UnitRay.Direction.Y, Mouse.UnitRay.Direction.Z) * CFrame.Angles(math.rad(10), math.rad(-168), math.rad(2)) + Vector3.new(SpawnCam.Position.X, SpawnCam.Position.Y, SpawnCam.Position.Z)
end)

Basically, whenever the LocalScript is deleted or disabled, this code bit continues to run.
I’ve tried incorporating an if-then statement (if script.DisableParallax.Value == false then & while script.DisableParallax.Value == false do), however these don’t make any difference.

There’s no errors, and it runs as if it were to never had been deleted.
I’ve been trying to fix this for several hours with a few breaks between.

Any help would be greatly appreciated!!

The mouse object belongs to the player instance not a single script though which it is accessed from.

But if the script weren’t to be running wouldn’t the mouse not be accessed?

In the same way that if a script was changing a Part’s BrickColor each second wouldn’t keep changing the Part’s colour after it was deleted

If you need to retain the position of the mouse at the time the script is disabled/destroyed then just use the following:

local finalMousePos = mouse.Hit.p
--script is disabled/destroyed here

You can use remote events/bindable events to send the data elsewhere before the script is no longer usable.

1 Like

The properties of the mouse change regardless of whether or not the mouse is being used within a script.

I don’t quite understand.
My goal is to have the mouse stop performing the snippet of code I put in the post when the LocalScript is disabled.

In that case, you can set a variable as the connection, and disconnect it before disabling the script.

Example

local connection



connection = Mouse.Move:Connect(function()

	Camera.CFrame = CFrame.new(Mouse.UnitRay.Direction.X, Mouse.UnitRay.Direction.Y, Mouse.UnitRay.Direction.Z) * CFrame.Angles(math.rad(10), math.rad(-168), math.rad(2)) + Vector3.new(SpawnCam.Position.X, SpawnCam.Position.Y, SpawnCam.Position.Z)

end)


--When you want to disable the function:

connection:Disconnect()
1 Like

Do you delete/disable it on server-side?

1 Like

Since the main menu is a GUI (I assume), it’s perfectly fine to delete the GUI with a local script, as Gui’s are local.

The disconnecting must be done in the same script as the MouseMove event though.

1 Like

This unfortunately didn’t work, I attached the Disconnect to a button and added a print() for debug (pictured below)

image

local connection

connection = Mouse.Move:Connect(function()
	Camera.CFrame = CFrame.new(Mouse.UnitRay.Direction.X, Mouse.UnitRay.Direction.Y, Mouse.UnitRay.Direction.Z) * CFrame.Angles(math.rad(10), math.rad(-168), math.rad(2)) + Vector3.new(SpawnCam.Position.X, SpawnCam.Position.Y, SpawnCam.Position.Z)
end)

script.Parent.MainMenu.menu.playb.MouseButton1Click:Connect(function()
	connection:Disconnect()
	print("Disconnected function!")
end)

Also, everything is handled using LocalScripts

I really thought that would work. I’ll have to look into it some more, I’ll let you know what I find.

Update:

So I’ve just made some sample code below, and after 5 seconds, the code inside the move function stops firing.

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local connection

connection = mouse.Move:Connect(function()
	print("hi")
end)


wait(5)

print("disconnecting function")
connection:Disconnect()

Is it possible that you have another script doing the exact same thing?

Put this code into a screen gui on a new baseplate. It should stop firing after the 5 seconds are up.

1 Like

This works, however, it doesn’t work when I make the Disconnect happen when clicking the button.

script.Parent.MainMenu.menu.playb.MouseButton1Click:Connect(function()
	connection:Disconnect()
	print("disconnected function!")
end)

It still shows that the button was clicked (output below) despite not Disconnecting.
image

By the way, I really appreciate the help! :grin:
Also, it’s getting late so I should get to sleep, I will be back in the morning
(For the record, I’m back now)

1 Like

This is because this is connected to a roblox script connection.

You should do this:

local Event = Mouse.Move:Connect(function()
	Camera.CFrame = CFrame.new(Mouse.UnitRay.Direction.X, Mouse.UnitRay.Direction.Y, Mouse.UnitRay.Direction.Z) * CFrame.Angles(math.rad(10), math.rad(-168), math.rad(2)) + Vector3.new(SpawnCam.Position.X, SpawnCam.Position.Y, SpawnCam.Position.Z)
end)

Event:Disconnect()

And you should make an event for this local script that makes it disconnect, like this:

local disconnect = event
local Event = Mouse.Move:Connect(function()
	Camera.CFrame = CFrame.new(Mouse.UnitRay.Direction.X, Mouse.UnitRay.Direction.Y, Mouse.UnitRay.Direction.Z) * CFrame.Angles(math.rad(10), math.rad(-168), math.rad(2)) + Vector3.new(SpawnCam.Position.X, SpawnCam.Position.Y, SpawnCam.Position.Z)
end)

disconnect.OnClientEvent:Connect(function()
	Event:Disconnect()
end()
1 Like

This unfortunately didn’t work, I used a BindableEvent named DisconnectEvent in the LocalScript.

local Disconnect = script.DisconnectEvent
local Event = Mouse.Move:Connect(function()
	Camera.CFrame = CFrame.new(Mouse.UnitRay.Direction.X, Mouse.UnitRay.Direction.Y, Mouse.UnitRay.Direction.Z) * CFrame.Angles(math.rad(10), math.rad(-168), math.rad(2)) + Vector3.new(SpawnCam.Position.X, SpawnCam.Position.Y, SpawnCam.Position.Z)
	print("a")
end)

script.Parent.MainMenu.menu.playb.MouseButton1Click:Connect(function()
	Disconnect:Fire()
	print("button clicked")
end)

Disconnect.Event:Connect(function()
	Event:Disconnect()
	print("disconnected function")
end)

It prints as expected, however, the Mouse.Move function continues to run after being ‘disconnected’ (it still prints “a” every time the mouse moves although the output reads that the function was disconnected).

This is interesting, why would you need an event for the disconnect, if you can just disconnect the event when the button is clicked? I see your Disconnect event as unnecessary.

1 Like

I tried integrating what you had sent me even if it seemed a bit unreasonable, for some reason just Disconnecting from clicking the button doesn’t work. There’s no error, it runs through the code, however the code it was supposed to stop continues running as well.

I FINALLY FOUND A FIX!

I think the issue was that it was all Local instead of Server-sided.
Inside of the local script, I put:

Mouse.Move:Connect(function()
	if script.EnableParallax.Value == true then
		Camera.CFrame = CFrame.new(Mouse.UnitRay.Direction.X, Mouse.UnitRay.Direction.Y, Mouse.UnitRay.Direction.Z) * CFrame.Angles(math.rad(10), math.rad(-168), math.rad(2)) + Vector3.new(SpawnCam.Position.X, SpawnCam.Position.Y, SpawnCam.Position.Z)
	end
end)

script.Parent.MainMenu.menu.playb.MouseButton1Click:Connect(function()
	script.DisconnectEvent:FireServer()
end)

… and in the server:

Disconnect.OnServerEvent:Connect(function()
	script.Parent.EnableParallax.Value = false
end)

It was as easy as that! Thank you to everyone who tried helping. :slightly_smiling_face: