How would i get the mouses position/movement direction, even when its off the screen

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

the basic explanation is in the title.
but heres it it with more detail.

what im trying to do is move a gui with the mouse even when its offscreen,
for example, is if you keep moving your mouse to the right, a gui would keep going in that direction, no matter how far you move right.

  1. What is the issue? Include screenshots / videos if possible!
    I dont know how to achieve this

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    iv tried using GetMouseLocation() and the Mouse.X/Y.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

I will answer any questions needed.

2 Likes

Could you just make the gui move with your mouse only when it is on the screen? What is the need

1 Like

the exact need is im trying to semi-recreate the Pandemonium minigame from “Pressure”
if you ever played or seen it, it has a circle that you can move with the mouse, even when the mouse isnt on screen. im trying to semi-recreate that circle movement

1 Like

I am pretty sure you cannot track the mouse’s position when it is not focused on Roblox/not inside the Roblox window.

1 Like

like the reply above, im trying to semi-replicate the minigame in pressure, meaning somthing on thos lines has been done before.

1 Like

I think that minigame uses a circle texture as a “fake mouse” that moves towards your actual mouse position while having a movement effect on it.

2 Likes

would you please explain in a little more detail?

1 Like

I was editing my previous response, but I’ll just make a new post as you have responded.

To recreate the Pandemonium mini-game, my first instincts would be to use a GUI to simulate the mouse, like what @DataSigh said. I would add some “weight” to it, meaning after calculating how far the mouse has moved, apply a factor and move that GUI x pixels in y direction. After a while, you’d just set the GUI back to the center. To “reset” the mouse to the center, you could try toggling between locking and unlocking the mouse in first person.

I personally have never played Pressure, but I did look at a quick Youtube video online and this is my best guess at how to recreate it.

4 Likes

reseting the mouse seems to be a good idea, thank you for the help so far!
im going to go quickly google how to do that and see what i can do

2 Likes

Steps i think you need to make the effect:

  1. Get a ImageLabel to follow the players mouse
  2. Add a lerp effect on the ImageLabel’s position when moving towards the mouse so there’s a small spring effect
  3. Make a function that returns a random 2d vector direction and a force variable
  4. Add that functions values on the ImageLabel’s position at given intervals
2 Likes

lerping really isnt my strong suit and ill have to look a little more into it, but thos seem like some things i can definitely try out! thank you!

1 Like

ive got the infinite mouse movment down but with one little side effect, when the mouse tps to center, the gui flashes to a spot before reseting to where it originally was.

the issue is that im using a delay(0) on turning off the mouse lock and reseting the pos, meaning that it dosent reset the pos instantly, if i do the reseting outside of the delay, nothing happens as it jsut reads the current pos as the currentposition, unlike while in the delay the curpos would be the position it was in before teleporting.

code im using below. (note that part of this code, [mostly the bound relations] is from a drag script i found a while back)
and if i dont use the delay function to turn off the mouse lock, it just dosent reset he position at all.
by any chance do you know a solution to this?

demo of the issue:

local UserInputService = game:GetService('UserInputService')

local frame = script.Parent
local leadFrame = Instance.new('Frame') do
	leadFrame.AnchorPoint = frame.AnchorPoint
	leadFrame.Position = frame.Position
	leadFrame.Size = frame.Size
	leadFrame.Name = `Lead {frame.Name}`
	leadFrame.Visible = false
	leadFrame.Parent = frame.Parent
end

local screenGui = frame:FindFirstAncestorOfClass('ScreenGui')

local inputChanged = nil
local inputEnded = nil
local mouse = game:GetService('Players').LocalPlayer:GetMouse()
local function getBoundsRelations(guiObject : GuiObject)
	local bounds = screenGui.AbsoluteSize
	local topLeft = screenGui.IgnoreGuiInset and guiObject.AbsolutePosition + Vector2.new(0, 36) or guiObject.AbsolutePosition
	local bottomRight = topLeft + guiObject.AbsoluteSize

	local boundRelations = {
		Top = topLeft.Y < 0 and math.abs(topLeft.Y) or nil,
		Left = topLeft.X < 0 and math.abs(topLeft.X) or nil,
		Right = bottomRight.X > bounds.X and math.abs(bottomRight.X - bounds.X) or nil,
		Bottom = bottomRight.Y > bounds.Y and math.abs(bottomRight.Y - bounds.Y) or nil,
	}

	return (not boundRelations.Top
		and not boundRelations.Bottom
		and not boundRelations.Left
		and not boundRelations.Right), boundRelations
end

local lastMousePosition = UserInputService:GetMouseLocation()
local goalPosition = frame.Position
local lastMouseX, lastMouseY = mouse.X, mouse.Y
local MouseDir 

 game:GetService("RunService").RenderStepped:Connect(function(delta)
	local currentMousePosition = UserInputService:GetMouseLocation()
	local mouse = game.Players.LocalPlayer:GetMouse()

	MouseDir = (Vector2.new(mouse.X,mouse.Y) - Vector2.new(lastMouseX,lastMouseY))
	lastMouseX, lastMouseY = mouse.X, mouse.Y
	local mouseDelta = currentMousePosition - lastMousePosition
	local ran = math.random(-10,10)
	local ran2 = math.random(-10,10)
	local offset = UDim2.new(0,MouseDir.X* 4,0,MouseDir.Y*4)
local curpos = script.Parent.Position
	delay(0,function()
		if mouse.X <= 50 or mouse.X >= 1170 or mouse.Y >= 430 or mouse.Y <= 10 then
			local Player = game.Players.LocalPlayer
			Player:SetAttribute("MouseLockEnabled", true) 
			delay(0,function()
				Player:SetAttribute("MouseLockEnabled", false)
				print(tostring(curpos).."    "..tostring(script.Parent.Position))

				script.Parent.Position = curpos
			end)
		end
	end)
	goalPosition = script.Parent.Position+ offset

	leadFrame.Position = goalPosition

	local isInBounds, relations = getBoundsRelations(leadFrame)

	if not isInBounds then
		local x = (relations.Left or 0) - (relations.Right or 0)
		local y = (relations.Top or 0) - (relations.Bottom or 0)



		goalPosition += UDim2.fromOffset(x, y)
	end

	frame.Position = goalPosition
	lastMousePosition = currentMousePosition
end)






frame.Destroying:Once(function()

	leadFrame = leadFrame:Destroy()

	if inputChanged  then
		inputChanged:Disconnect()
		inputChanged = nil
	end

	if inputEnded then
		inputEnded:Disconnect()
		inputEnded = nil
	end
end)```
1 Like

I’m pretty sure that “flashing” is related to how you are moving the GUI. When the mouse position resets, I think your script calculates the difference in the previous coordinates and the new (reset) coordinates.

I’m not an expert in this field, but one suggestion would be to set the UI’s position to the center and temporarily disable the movement code before and after the mouse moves.

disabling it just made the reseting for some reason jsut stop working in general. but i did find a weird effect, when the mouse pos resets, the square seems to use the opposite of the access it was reset from
example: reseting from the sides will make the gui reset to the same Y access but flipped X (other way arround for up and down resets)

sorry for the double reply but, Nevermind! it truns out i needs the oter delay (surronding the if statement) or else it jsut didnt work. and now it works fine without the flash! now im going to try using the force direction

1 Like

Last reply unless you ask.
Ive got a decent basic version working!!
source code: (Local) (also put under the frame u want moving)

local UserInputService = game:GetService('UserInputService')

local frame = script.Parent
local leadFrame = Instance.new('Frame') do
	leadFrame.AnchorPoint = frame.AnchorPoint
	leadFrame.Position = frame.Position
	leadFrame.Size = frame.Size
	leadFrame.Name = `Lead {frame.Name}`
	leadFrame.Visible = false
	leadFrame.Parent = frame.Parent
end

local screenGui = frame:FindFirstAncestorOfClass('ScreenGui')

local inputChanged = nil
local inputEnded = nil
local mouse = game:GetService('Players').LocalPlayer:GetMouse()
local function getBoundsRelations(guiObject : GuiObject)
	local bounds = screenGui.AbsoluteSize
	local topLeft = screenGui.IgnoreGuiInset and guiObject.AbsolutePosition + Vector2.new(0, 36) or guiObject.AbsolutePosition
	local bottomRight = topLeft + guiObject.AbsoluteSize

	local boundRelations = {
		Top = topLeft.Y < 0 and math.abs(topLeft.Y) or nil,
		Left = topLeft.X < 0 and math.abs(topLeft.X) or nil,
		Right = bottomRight.X > bounds.X and math.abs(bottomRight.X - bounds.X) or nil,
		Bottom = bottomRight.Y > bounds.Y and math.abs(bottomRight.Y - bounds.Y) or nil,
	}

	return (not boundRelations.Top
		and not boundRelations.Bottom
		and not boundRelations.Left
		and not boundRelations.Right), boundRelations
end

local lastMousePosition = UserInputService:GetMouseLocation()
local goalPosition = frame.Position
local lastMouseX, lastMouseY = mouse.X, mouse.Y
local MouseDir 
local doDec = true
UserInputService.MouseIconEnabled = false
 game:GetService("RunService").RenderStepped:Connect(function(delta)
		if not doDec then
			return
		end
	local currentMousePosition = UserInputService:GetMouseLocation()
	local mouse = game.Players.LocalPlayer:GetMouse()

	MouseDir = (Vector2.new(mouse.X,mouse.Y) - Vector2.new(lastMouseX,lastMouseY))
	lastMouseX, lastMouseY = mouse.X, mouse.Y
	local mouseDelta = currentMousePosition - lastMousePosition
	local ran = math.random(-10,10)
	local ran2 = math.random(-10,10)
	
	
	local knockChance = math.random(1,200)
	local force = 1000
	if knockChance == 1 then
		local other = math.random(1,4)
		if other == 1 then
			ran2 += force
			ran += force
		elseif other == 2 then
			ran2 += -force
			ran += force
		elseif other == 3 then
			ran2 += -force
			ran += -force
		elseif other == 4 then
			ran2 += force
			ran += -force
		end
	end
	local offset = UDim2.new(0,MouseDir.X* 4+ran,0,MouseDir.Y*4+ran2)
 local curpos = script.Parent.Position
	delay(0,function()  
		if mouse.X <= 50 or mouse.X >= 1170 or mouse.Y >= 430 or mouse.Y <= 10 then
			doDec = false
			local Player = game.Players.LocalPlayer

			Player:SetAttribute("MouseLockEnabled", true) 
			delay(0,function()
				Player:SetAttribute("MouseLockEnabled", false)
				script.Parent.Position = curpos
				doDec = true
			end)
			end
		end)
	goalPosition = script.Parent.Position+ offset

	leadFrame.Position = goalPosition

	local isInBounds, relations = getBoundsRelations(leadFrame)

	if not isInBounds then
		local x = (relations.Left or 0) - (relations.Right or 0)
		local y = (relations.Top or 0) - (relations.Bottom or 0)



		goalPosition += UDim2.fromOffset(x, y)
	end

	--frame.Position = goalPosition
	if knockChance~= 1 then
	frame.Position = script.Parent.Position:Lerp(goalPosition,1)
	else
		local tween = game:GetService("TweenService"):Create(frame,TweenInfo.new(0.05),{Position = goalPosition})
		tween:Play()
		tween.Completed:Wait()
		--frame.Position = script.Parent.Position:Lerp(goalPosition,1)
	end
	lastMousePosition = currentMousePosition
end)






frame.Destroying:Once(function()

	leadFrame = leadFrame:Destroy()

	if inputChanged  then
		inputChanged:Disconnect()
		inputChanged = nil
	end

	if inputEnded then
		inputEnded:Disconnect()
		inputEnded = nil
	end
end)
1 Like

Glad you got it to work :slight_smile: (would be appreciated if you could mark one of the posts here as the Solution)

1 Like

ill use your since you were the most help (and most used as refrence) Thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.