How can I get a TextLabel to follow the player's mouse using Scale, not pixels?

Hello.

I’m trying to get a TextLabel to follow a player’s mouse, and kind of succeeded but it doesn’t work properly. Here is a video of what I mean:

https://gyazo.com/c559431cd5d9d8e163e21e6090d838e1

My code:

con = mouse.Move:Connect(function()
	local scaleX = (mouse.X / workspace.CurrentCamera.ViewportSize.X) 
	local scaleY = (mouse.Y / workspace.CurrentCamera.ViewportSize.Y) 
	label.Position = UDim2.new(scaleX, 0, scaleY, 0)
end)

Any help is greatly appreciated, thank you. :slight_smile:

1 Like

I think your code should be working correctly…

Is the parent of the textlabel somehow not sized to be around the entire screen?

1 Like

The parent of the TextLabel is not the size of the whole screen, I think that may be the issue… How can I fix this?

Open the propertys tab and set the “Size” property of the textlabel’s parents to {1, 0, 1, 0}, and the parent of the textlabel’s parent, and the parent of that too…till you reach startergui or playergui.

1 Like

I need the GUI to be the size it is. How can I make it work even though it is not {1, 0, 1, 0}?

Then maybe put it in another GUI object that you can afford to be sized to full screen, and fix all the hierarchy changes in your scripts.

And if you don’t want to do that either, just put a useless empty GUI object that you are not going to do anything to, and size it to be full screen, and then replace your code with this:

local uselessGUI = Blablablawhereveryouputit
con = mouse.Move:Connect(function()
    local screenSize = uselessGUI.AbsoluteSize
	local scaleX = (mouse.X / workspace.CurrentCamera.ViewportSize.X) 
	local scaleY = (mouse.Y / workspace.CurrentCamera.ViewportSize.Y) 
	label.Position = UDim2.new(0, scaleX * screenSize.X, 0, scaleY * screenSize.Y)
end)

Hope this helped!
Edit: Changed code to be compliant with varying screen sizes during a session(such as when window is restored down)

This still leaves the gui’s position in offset; I need it as scale. Any way to change that? :confused:

I made sliders a very long time ago that required this.
Basically all you need is this code:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local BackgroundFrame = script.Parent.Background

local RelXOnGui = (1 / (BackgroundFrame.AbsoluteSize.X / mouse.ViewSizeX)) * (mouse.X - BackgroundFrame.AbsolutePosition.X) / mouse.ViewSizeX
local RelYOnGui = (1 / (BackgroundFrame.AbsoluteSize.Y / mouse.ViewSizeY)) * (mouse.Y - BackgroundFrame.AbsolutePosition.Y) / mouse.ViewSizeY
2 Likes

Thank you so much! :slight_smile:

1 Like