Need help with a Simulated Computer Mouse using SurfaceGui

I’m making a game where you have to maintain a rocket’s condition until it reaches space. I decided to make a DragDetector simulated mouse, and the positioning works PERFECTLY, but I’m not sure how to make it click stuff. I searched a lot for a problem like this, and found nothing, so I’m making a post.

I thought about making the REAL mouse move to the fake mouse’s position to click a Gui element, but I have no idea how to move the real mouse either. Any help is appreciated!

3 Likes

Perhaps your best bet would be to simulate the click. Rather than using Roblox’s built in GUI events just detect any mouse click and check the position of the virtual mouse. If the virtual mouse is intersecting the button, show the corresponding window.

1 Like

In Roblox, directly moving the real mouse isn’t possible due to security reasons. However, you can simulate mouse clicks by using UserInputService and GUI elements.

-- Place this script in a LocalScript inside StarterPlayerScripts or StarterGui

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = player:GetMouse()
local DragDetector = script.Parent:WaitForChild("DragDetector") --Your simulated mouse GUI element
local Dragging = false

local function UpdateDragDetectorPosition()
    DragDetector.Position = UDim2.new(0, Mouse.X, 0, Mouse.Y)
end

local function CheckGuiElementClick()
    local ScreenGui = Player:WaitForChild("PlayerGui"):FindFirstChild("YourScreenGui") --Replace with your ScreenGui name
    if not ScreenGui then return end
	
    for _, GuiElement in pairs(ScreenGui:GetDescendants()) do
        if GuiElement:IsA("TextButton") or GuiElement:IsA("ImageButton") then
            if GuiElement.AbsolutePosition.X <= DragDetector.AbsolutePosition.X and GuiElement.AbsolutePosition.Y <= DragDetector.AbsolutePosition.Y and GuiElement.AbsolutePosition.X + GuiElement.AbsoluteSize.X >= DragDetector.AbsolutePosition.X and GuiElement.AbsolutePosition.Y + GuiElement.AbsoluteSize.Y >= DragDetector.AbsolutePosition.Y then
                GuiElement:Activate()
                break
            end
        end
    end
end

UserInputService.InputBegan:Connect(function(Input, GameProcessed)
    if GameProcessed then return end
    
    if Input.UserInputType == Enum.UserInputType.MouseButton1 then
        Dragging = true
        UpdateDragDetectorPosition()
    end
end)

UserInputService.InputChanged:Connect(function(Input, GameProcessed)
    if GameProcessed then return end
    
    if Input.UserInputType == Enum.UserInputType.MouseMovement and Dragging then
        UpdateDragDetectorPosition()
    end
end)

UserInputService.InputEnded:Connect(function(Input, GameProcessed)
    if GameProcessed then return end
	
    if Input.UserInputType == Enum.UserInputType.MouseButton1 then
        Dragging = false
        CheckGuiElementClick()
    end
end)
3 Likes

I’m using a SurfaceGui, but this worked anyway. Thank you!!

1 Like

Update, TextButton:Activate doesn’t exist. I could’ve sworn it did. I tested this on screengui’s and surfacegui’s and it didnt even exist.

You can always write a custom event.

local GuiElementEvent = GuiElement:FindFirstChildOfClass("BindableEvent")

if GuiElementEvent ~= nil then
    GuiElementEvent:Fire()
end

-- somewhere
guiElement:FindFirstChildOfClass("BindableEvent").Event:Connect(function()
    -- psuedo activate
end)

Ohhh, funny so did I! Try GuiElement.Interactable = true