Focus Mouse on UI

Hello, I would like to know how may I focus the mouse of the player on my UI and not on the player. I mean, when I press my right click (until I stop pressing it) I would like to have my mouse focusing on my UI like this I can select “a sort”.
The code of my tool:

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")

local selectedSort = nil

local debounce = true -- cooldown check
local cooldown = 0.5 -- cooldown

local debounce_ui = true -- cooldown check
local cooldown_ui = 0.81 -- cooldown

local isEquipped = false

local function createParticleEmitter(color, parent)
	local particleEmitter = Instance.new("ParticleEmitter")
	particleEmitter.Color = ColorSequence.new(color)
	particleEmitter.LightEmission = 1
	particleEmitter.LightInfluence = 1
	particleEmitter.Orientation = "FacingCamera"
	particleEmitter.Size = NumberSequence.new(0.2)
	particleEmitter.Squash = NumberSequence.new(0)
	particleEmitter.Texture = "http://www.roblox.com/asset/?id=243728166"
	particleEmitter.Transparency = NumberSequence.new(0.5)
	particleEmitter.Rate = 100
	particleEmitter.EmissionDirection = "Top"
	particleEmitter.Lifetime = NumberRange.new(0.3,0.3)
	particleEmitter.RotSpeed = NumberRange.new(0.2)
	particleEmitter.Speed = NumberRange.new(1)
	particleEmitter.SpreadAngle = Vector2.new(1, 1)
	particleEmitter.Shape = "Cylinder"
	particleEmitter.ShapeInOut = "Outward"
	particleEmitter.ShapePartial = 1
	particleEmitter.ShapeStyle = "Surface"
	particleEmitter.TimeScale = 1
	particleEmitter.Parent = parent
end

local function createPart(color, beam)
	local part = Instance.new("Part")
	part.Parent = workspace
	part.Name = "PartEffetBeamSort"
	part.Anchored = true
	part.CanCollide = false
	local direction = (beam.Attachment0.WorldCFrame.Position - beam.Attachment1.WorldCFrame.Position).Unit
	part.CFrame = beam.Attachment0.WorldCFrame:Lerp(beam.Attachment1.WorldCFrame, 0.5)
	part.CFrame = CFrame.lookAt(part.Position, part.Position + direction)
	part.Size = Vector3.new(0.5, 0.5, (beam.Attachment0.WorldCFrame.Position - beam.Attachment1.WorldCFrame.Position).Magnitude)
	part.Transparency = 1
	createParticleEmitter(color, part)
	createParticleEmitter(color, part)
	createParticleEmitter(color, part)
end

local function createBeam(color, attachment1)
	local beam = Instance.new("Beam")
	beam.Color = ColorSequence.new(color)
	local attachment0 = Instance.new("Attachment", handle.EndBaguette)
	beam.Attachment0 = attachment0
	beam.Attachment1 = attachment1
	beam.FaceCamera = true
	beam.LightEmission = 0.5
	beam.Transparency = NumberSequence.new(0.5)
	beam.Width0 = 0.2
	beam.Width1 = 0.2
	beam.Parent = workspace
	createPart(color, beam)
	return beam
end

tool.Unequipped:Connect(function()
	isEquipped = false
end)

tool.Equipped:Connect(function()
	isEquipped = true
	
	mouse.Button1Down:Connect(function()
		if isEquipped then
			local ray = Ray.new(plr.Character.HumanoidRootPart.Position, (mouse.Hit.p - plr.Character.HumanoidRootPart.Position).unit * 300)
			local part, position = game.Workspace:FindPartOnRay(ray, plr.Character)

			if part and part.Parent then
				local character = part.Parent
				local targetHRP = character:FindFirstChild("HumanoidRootPart")
				if targetHRP and debounce then
					local color = Color3.fromRGB(125, 125, 125)
					local attachment1 = Instance.new("Attachment", targetHRP)
					local beam = createBeam(color, attachment1)
					wait(0.1)
					game:GetService("ReplicatedStorage").Events.DamagePlayer:FireServer(targetHRP.Parent:FindFirstChild("Humanoid"), 10)
					beam:Destroy()
					workspace:FindFirstChild("PartEffetBeamSort"):Destroy()
					attachment1:Destroy()
					handle.EndBaguette:FindFirstChild("Attachment"):Destroy()
					debounce = false
					wait(cooldown)
					debounce = true
				end
			end
		end
	end)


	mouse.Button2Down:Connect(function()
		if isEquipped then
			plr.PlayerGui.SortsSelection.Status.Value = 1
		end
	end)

	mouse.Button2Up:Connect(function()
		if isEquipped then
			plr.PlayerGui.SortsSelection.Status.Value = 0
		end
	end)

	mouse.Move:Connect(function()
		if isEquipped and plr.PlayerGui.SortsSelection.Status.Value == 1 then
			for i = 1, 4 do
				local sort = plr.PlayerGui.SortsSelection:FindFirstChild("Sort"..i)
				if sort then
					local absPos = sort.AbsolutePosition
					local absSize = sort.AbsoluteSize
					if mouse.X >= absPos.X and mouse.X <= absPos.X + absSize.X and mouse.Y >= absPos.Y and mouse.Y <= absPos.Y + absSize.Y then
						sort.BackgroundColor3 = Color3.fromRGB(0, 61, 0)
						selectedSort = sort.Name
					else
						sort.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
					end
				end
			end
		end
	end)
end)

Code from the UI:

local ui = script.Parent
local status = ui.Status

local maxSorts = 4

local function resetVisibleSorts(bool)
	for i = 1, maxSorts, 1 do
		local s = ui:FindFirstChild("Sort"..i)
		s.Visible = bool
		s.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
	end
end

status:GetPropertyChangedSignal("Value"):Connect(function()
	if status.Value == 0 then -- closing the UI
		resetVisibleSorts(true) 
		
		for i = maxSorts, 1, -1 do
			local s = ui:FindFirstChild("Sort"..i)
			s.GroupTransparency = 0
			for j = 0, 1, 0.1 do
				s.GroupTransparency = j
				task.wait(0.001)
			end
			s.Visible = false
			task.wait(0.05)
		end
		ui.Enabled = false
	elseif status.Value == 1 then -- opening the UI
		resetVisibleSorts(false) 
		
		ui.Enabled = true
		
		for i = 1, maxSorts, 1 do
			local s = ui:FindFirstChild("Sort"..i)
			s.Visible = true
			s.GroupTransparency = 1
			for j = 1, 0, -0.1 do
				s.GroupTransparency = j
				task.wait(0.001)
			end
			task.wait(0.05)
		end
	end
end)

Workspace:

Video: https://gyazo.com/938843e4384554854bf72d5f28878eee

You are probably looking for PlayerModule’s controls. PlayerModule is a module that automatically adds itself into a Player.PlayerScripts folder. While I don’t know much about what excactly it does, but doing the following might be what you want (must be running locally obviously)

local plr = game:GetService('Players').LocalPlayer
local playerModule = require(plr.PlayerScripts.PlayerModule)
local controlsModue = playerModule:GetControls()

-- Disabling controls
controlsModue:Disable()

-- Enabling controls
controlsModue:Enable()

This PlayerModule is only visible when the game is running. You can copy it and then have a proper look as it

It does not solve the problem sadly :(.

So instead of it rotating my view I want to be able to move my mouse “in my UI”.

So by holding right click you want your mouse to not move the camera? That’s excactly what the code above dose
Apologies, I am wrong. I looked at my old code and thougt it was doing as commented there lol. The way I worked around looks like this

local contextActionService = game:GetService('ContextActionService')

-- Disabling camera movement
contextActionService:BindAction('BlockingInput', function() end, true, Enum.UserInputType.MouseButton2)

-- Enabling it back
contextActionService:UnbindAction('BlockingInput')

However my game uses right mouse button so I actually need this functions and I am unsure how it behaves with UI. You can try this one out

1 Like

I tried it before but when I am in UI, when I stop pressing on my right click, it does not execute the “close” of the UI.

But if you have another idea that use a frame, I take it :).

With my previous suggestion, try replacing your mouse.MouseButton2Up and mouse.MouseButton2Down with this

UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
	if not isEquipped or input.UserInputType ~= Enum.UserInputType.MouseButton2 then return end
	-- isn't a right mouse button
		
	print('MouseButton2 Down')
end)

UserInputService.InputEnded:Connect(function(input: InputObject, gameProcessedEvent: boolean)
	if not isEquipped or input.UserInputType ~= Enum.UserInputType.MouseButton2 then return end
	-- isn't a right mouse button

	print('MouseButton2 Up')
end)
1 Like

Still turning :frowning:
https://gyazo.com/cacd3ada98ddd2d3daea9b059ba4f65d

local ContextActionService = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")

local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")

local selectedSort = "Sort1"

local debounce = true -- cooldown check
local cooldown = 0.5 -- cooldown

local debounce_ui = true -- cooldown check
local cooldown_ui = 0.81 -- cooldown

local isEquipped = false

tool.Unequipped:Connect(function()
	isEquipped = false
end)

tool.Equipped:Connect(function()
	isEquipped = true
	
	mouse.Button1Down:Connect(function()
		if isEquipped then
			local ray = Ray.new(plr.Character.HumanoidRootPart.Position, (mouse.Hit.p - plr.Character.HumanoidRootPart.Position).unit * 300)
			local part, position = game.Workspace:FindPartOnRay(ray, plr.Character)

			if part and part.Parent then
				local character = part.Parent
				local targetHRP = character:FindFirstChild("HumanoidRootPart")
				if targetHRP and debounce then
					print(selectedSort)
					if selectedSort == "Sort1" then
						game:GetService("ReplicatedStorage").Events.Sorts.Impactus:FireServer(handle, targetHRP)
						game:GetService("ReplicatedStorage").Events.DamagePlayer:FireServer(targetHRP.Parent:FindFirstChild("Humanoid"), 10)
					end
					debounce = false
					wait(cooldown)
					debounce = true
				end
			end
		end
	end)


	UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
		if not isEquipped or input.UserInputType ~= Enum.UserInputType.MouseButton2 then return end
		
		plr.PlayerGui.SortsSelection.Status.Value = 1
		ContextActionService:BindAction('BlockingInput', function() end, true, Enum.UserInputType.MouseButton2)
	end)

	UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
		if not isEquipped or input.UserInputType ~= Enum.UserInputType.MouseButton2 then return end
		
		wait(0.5)
		plr.PlayerGui.SortsSelection.Status.Value = 0
		ContextActionService:UnbindAction('BlockingInput')
	end)
end)

You could also try to make invisible TextButton scaled to the whole screen as it should block right mouse button input. Make it appear when a tool is equipped and disappear when tool is unequipped (I guess you generally don’t want players to rotate camera when tool is equipped)

Tried as well, same with a frame, the problem is that if I stop pressing my right click, my UI don’t close.

And btw, I want that they can rotate camera when the UI is not open, not when the tool is Equipped.

Then if I had to put a key on the keyboard rather than right click, what is the most intuitive for you?