MouseUnlock Script only works on some DragDetectors

local MouseUnlock = {}

function MouseUnlock.Start()
	print("Started MouseUnlock Module")
	
	local Players = game:GetService("Players")
	local RunService = game:GetService("RunService")
	local UserInputService = game:GetService("UserInputService")
	local CollectionService = game:GetService("CollectionService")

	local player = Players.LocalPlayer

	local mouseUnlocked = Instance.new("BoolValue", player)
	mouseUnlocked.Name = "MouseUnlocked"
	mouseUnlocked.Value = false
	
	for _, v in ipairs(CollectionService:GetTagged("Draggable")) do
		print("1st iteration")
			if v:IsA("DragDetector") then
				print("2nd if statement")
				v.DragStart:Connect(function(player)
					mouseUnlocked.Value = true
				end)
				v.DragEnd:Connect(function()
					mouseUnlocked.Value = false
				end)
		end
	end

	RunService:BindToRenderStep("MouseBehaviorCustom", Enum.RenderPriority.Camera.Value + 1, function()
		if mouseUnlocked.Value then
			UserInputService.MouseBehavior = Enum.MouseBehavior.Default
		end
	end)
	
end

return MouseUnlock

image
image
Hi all, this is a script where the mouse unlocks when using a drag detector. The drag detectors are located inside MainPart and are tagged Draggable. This however only works on some drag detectors, when I use some drag detectors, the mouse unlocking doesn’t work, but on some it does work. I’m not sure what the issue is and was wondering someone could help. :wink:

When game starts how far from player “not working” grag detectors? Replication can be the reason why collection service doesn’t return all detectors.
Set them meaningful names and print in the cycle, that would help figure out missed ones.

It seems that the DragDetectors located closest to me work so I believe you’re right about that, how could I fix it?

this version handles streaming:

--!strict

local MouseUnlock = {}

function MouseUnlock.Start()
	print("Started MouseUnlock Module")

	local Players = game:GetService("Players")
	local RunService = game:GetService("RunService")
	local UserInputService = game:GetService("UserInputService")
	local CollectionService = game:GetService("CollectionService")

	local player = Players.LocalPlayer

	local mouseUnlocked = Instance.new("BoolValue", player)
	mouseUnlocked.Name = "MouseUnlocked"
	mouseUnlocked.Value = false
	
	local connected: {[Instance]: {RBXScriptConnection}} = {}
	local function initDragDetectot(v: Instance)

		if not v:IsA("DragDetector") then return end
		if connected[v] then return end

		local connections = {}

		v.AncestryChanged:Once(function(child: Instance, parent: Instance)
			for _, c in connected[v] do
				c:Disconnect()
			end
			connected[v] = nil
		end)

		table.insert(connections, v.DragStart:Connect(function(player)
			print("drag start", v)
			mouseUnlocked.Value = true
		end))

		table.insert(connections, v.DragEnd:Connect(function()
			print("drag end", v)
			mouseUnlocked.Value = false
		end))

		connected[v] = connections		
	end

	for _, v in CollectionService:GetTagged("Draggable") do
		initDragDetectot(v)
	end
	workspace.DescendantAdded:Connect(function(descendant: Instance)
		if not descendant:HasTag("Draggable") then return end
		initDragDetectot(descendant)
	end)

	RunService:BindToRenderStep("MouseBehaviorCustom", Enum.RenderPriority.Camera.Value + 1, function()
		if mouseUnlocked.Value then
			UserInputService.MouseBehavior = Enum.MouseBehavior.Default
		end
	end)
end

return MouseUnlock
1 Like