Unable to Automate Keypresses on Roblox

I was messing around in a Roblox game testing out a simple click script I made using Python that just repeatedly clicked while the player was in the game. I noticed that even though the mouse went to the desired location, the mouse icon didn’t change to indicate that the button was clickable and it didn’t appear to be clicking the button. I tried clicking multiple times without moving my mouse but it still didn’t process. After a bit, I moved my mouse and clicked it and it worked. I wondered why this had happened and tried it again. Same result, only working after I moved it myself. To see if this was something happening only on the Roblox client or Windows, I tried it on another app and it worked.

Does Roblox have anti-automation to detect whether keypresses are legitimate? I tried writing a script that randomized mouse movements on a path to the destination and It still wasn’t able to click.

from pynput.mouse import Listener
import pyautogui
import keyboard
import random
import time

x, y = pyautogui.size()


def on_click(mx, my, button, pressed):
    global x, y
    print(x/mx, y/my)

def get_location():
    with Listener(on_click=on_click) as listener:
        listener.join()

def click_ratio(xr, yr):
    global x, y
    time.sleep(0.05)

    mx, my = pyautogui.position()
    slope = (abs(abs(y/yr)-my))/(abs(x/xr)-mx)
    y_intercept = my-(slope*mx)

    print(f'Slope: {slope}\nY-Intercept: {y_intercept}')
    ratio = (abs(x/xr)-mx) / 10
    print(f'Ratio: {ratio}\nEnd Coordinates: ({(x/xr)}, {(y/yr)})')

    points = []
    c = 1
    for i in range(10):
        if my < y / 2:
            yl = slope*(mx+(c*ratio))+(y_intercept+float(f'0.{random.randint(1, 9)}'))
        else:
            yl = slope*(mx-(c*ratio))+(y_intercept+float(f'0.{random.randint(1, 9)}'))
        points.append((mx+(c*ratio), yl))
        c += 1

    for point in points:
        xloc, yloc = point
        pyautogui.moveTo(xloc, yloc)

    for i in range(10):
        pyautogui.click(x/xr+random.randint(-2,2), y/yr+random.randint(-2,2))
    time.sleep(0.05)

def surface():
    click_ratio(1.0491803278688525, 1.6981132075471699)


if __name__ == '__main__':
    # get_location()
    while True:
        try:
            if keyboard.is_pressed('z'):
                break
        except KeyError: pass

    # main loop
    time.sleep(0.5)

    surface()

Well HOPEFULLY, otherwise people can use this script maliciously to make people buy game passes without the player’s consent.

I wouldn’t ask this on devforum, and I haven’t used python in a long time, but I think this has something to do with the tab of Roblox not being full-on selected. But, I don’t believe Roblox has anti-automation.

Python isn’t available on Roblox. This is probably an application they made trying to automate something on a game.

I’ve also noticed the frame selection problem but that doesn’t seem isolated to Roblox, on windows 11 it seems like every app needs 2+ clicks and a bit of movement to focus the frame

oh my bad, I thought this was roblox lol

This is because the application isn’t focused and needs to be clicked on to focus on the application. So, you probably just need to have it click twice.

The script starts off with a keypress, I manually focus the frame before it.

while True:
        try:
            if keyboard.is_pressed('z'):
                break
        except KeyError: pass

I recommend using pyinput to get the mouse and mouseButton.

from pynput.mouse import Button as MouseButton, Controller
mouse = Controller()
mouse.position = ##wherever
sleep(0.1)
mouse.press(MouseButton.left)
mouse.release(MouseButton.left)

I tried that already in the beginning but I also just tried it again and it doesn’t work. It goes to the location and then nothing happens. Even if I click it afterwards, it does nothing until I move the mouse a little bit.