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: