Make a custom mouse pointer on a surfacegui

Hello!

I’m trying to make a computer monitor in my game that can be interacted with through a surfacegui. I’m trying to make an imagelabel follow the mouse on the surfacegui as well. I sort of have working code, but it has some really weird behavior when i move the mouse too close to the egde of the surfacegui (especially on the top and right edges), its very choppy, and i have a warning about using heartbeat in a localscript, but i have no idea how i’d get the mouse location from a server sided script.

I feel like there has to be a more straightforward way to do this, so any help is appreciated. Below is my current code.

local player = game:GetService("Players").LocalPlayer
local SurfaceGui = player.PlayerGui:WaitForChild("SurfaceGui")
local Mouse = player:GetMouse()
local Pointer = SurfaceGui:FindFirstChild("MousePointer")

local RS = game:GetService("RunService")

local GuiParentPart = SurfaceGui.Adornee


RS.Heartbeat:Connect(function()
	local TopLeftCFrame = GuiParentPart.CFrame * CFrame.new(GuiParentPart.Size.X / 2, GuiParentPart.Size.Y / 2, GuiParentPart.Size.Z / 2)
	local MouseDistanceFromTopLeft = TopLeftCFrame.Position - Mouse.Hit.Position
	local RelativeLocation = Vector2.new(math.abs(MouseDistanceFromTopLeft.X) * SurfaceGui.PixelsPerStud, math.abs(MouseDistanceFromTopLeft.Y) * SurfaceGui.PixelsPerStud)
	Pointer.Position = UDim2.new(0, RelativeLocation.X, 0, RelativeLocation.Y)
	task.wait()
end)

This is what it does:

Thank you!!!

i think you might be getting the warning because you’re doing task.wait() inside of the heartbeat. Nevermind, it’s because you’re not using an actual local script. you’re using a server script set to client run context. switch it to a local script.

Tried that and was still getting the warning :frowning:

are you sure? local scripts don’t even have the run context option. ur warnig is talking abt run context so…
there’s no reason why it would give a warning. you should also remove that task.wait()

Are you insane?
Dude wth of everengineering are you doing?
You are not breaking the matrix, pal; you are breaking common sense.

That a common sense to make client operation ON CLIENT.
Geez dude its literally roblox the only platform where people are doing this nonsense like you rn cause network is already setup for you :man_facepalming:

People should stop watching 30 minute tutorials and thinking that they can go into production so early.
simple as that.

I knew that already. The reason i said that is because of the warning specifying that the run context should be legacy for the heartbeat thing (So no, a LocalScript won’t fix that.) I should have phrased that better though.

Basically the issue I’m having with the warning is that there is no way to make the mouse location function occur on a server script with legacy context (because mouse is client sided), but the warning says that unexpected behavior can occur with the RunService heartbeat loop on client sided scripts.

Like I said, I feel like I’m missing something obvious because surely there must be a way for a SurfaceGui to get the mouse’s position on itself. That’s the bigger issue I think, since even if I find a way to make this current setup work without warnings, that still leaves all of the weird behavior when the mouse is near the edge of the screen, which I don’t even have an idea of how to fix. So a completely different solution might be ideal.

Thanks!

Edit: If the above is truly impossible I’m willing to just show the normal mouse and not do an imagelabel mouse pointer thing. I just thought it would be cool to have the mouse pointer for this interface be actually ON the screen of the monitor.

Removing task.wait fixed the choppiness so thats good!! Still getting that weird drift/blinking/moving in reverse for somereason thing at the edges of the screen though. I feel like i might need to completely redo this because its being really odd. Thank you!

Why dont you just use PreRender? RunService | Documentation - Roblox Creator Hub

Same warning. It doesn’t like something about using RunService stuff on client sided scripts I guess. I can do a while true do but that requires a task.wait which brings back the choppiness a bit, might look into a different setup altogether. Thank you!

What exact error is heppening?
It could be a potential bug report maybe.
Please send error message .

bro–i read the warning. it has nothing to do with runservice its talking about non-legacy run context, meaning you have a normal script set to run on the client The error is not talking about running heartbeat on the client–that’s a perfectly normal thing to do. The issue is that you are using a normal script not a local script inside of a place like starterplayerscripts. Transfer your code to a fresh local script and the error will be gone. In the future, please do a bit of research before disreguarding posts/replies. A simple chatgpt or google search of your error would have gotten you this simple fix.

I got inspired from this project and decided to try it out myself.
Mouse.hit is something very old and the issue there is that the position on the edges is nil or the character itself collides with the ray and that causes this behavior.
Here is what I made: :grin:

The code:

local svRun = game:GetService("RunService")
local svUIS = game:GetService("UserInputService")
local svTween = game:GetService("TweenService")

local partScreen = workspace:WaitForChild("Screen")
local SurfaceGUI = partScreen.SurfaceGui
local imgMouse = SurfaceGUI.Mouse

local vec2UpLeft = Vector2.new(partScreen.Position.X, partScreen.Position.Y) - Vector2.new(partScreen.Size.X/2, partScreen.Size.Y/2)
local vec2DownRight = Vector2.new(partScreen.Position.X, partScreen.Position.Y) + Vector2.new(partScreen.Size.X/2, partScreen.Size.Y/2)

local function Offset(a, b, c)
	return (c-a) / (b-a)
end

local raycastparams = RaycastParams.new()
raycastparams.FilterType = Enum.RaycastFilterType.Include
raycastparams.FilterDescendantsInstances = {partScreen}

local function loop()
	local vec2MouseLoc = svUIS:GetMouseLocation()
	local mouseray = workspace.Camera:ViewportPointToRay(vec2MouseLoc.X, vec2MouseLoc.Y)
	local raycast = workspace:Raycast(mouseray.Origin, mouseray.Direction * 1000, raycastparams)
	
	if(raycast == nil) then
		return
	end
	
	if(raycast.Instance ~= partScreen) then
		return
	end
	
	local vec3 = raycast.Position
	
	
	local numXScale = Offset(vec2UpLeft.X, vec2DownRight.X, vec3.X)
	local numYScale = Offset(vec2DownRight.Y, vec2UpLeft.Y, vec3.Y)
	
	svTween:Create(imgMouse, TweenInfo.new(.1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {Position = UDim2.new(numXScale, 0, numYScale, 0)}):Play()
end
svRun:BindToRenderStep("loop", 4, loop)

Feel free to use it :grinning_face:
Here is model: https://create.roblox.com/store/asset/73724733595095/Screen

Alright, sorry about that. I’m kind of restructuring the whole thing now with the help of someone in a discord server I’m in so I’ll let yall know how that goes. Thanks!

Thank you for this!! Right now I’m looking into a solution using InputChanged but if that goes nowhere I’ll absolutely try this next. Thank you!

With the help of some very very very awesome people in a discord server im in i am proud to say I HAVE A WORKING SCRIPT!!!

local Players = game:GetService("Players")
local GuiService = game:GetService("GuiService")

local player = Players.LocalPlayer

local function onCharacterAdded(character: Model) --unused variable dw about it
	local surfaceGui = player.PlayerGui:WaitForChild("SurfaceGui")

	local mainFrame = surfaceGui.BackgroundOpen
	local pointer = surfaceGui.MousePointer
	local insetTop, insetBottom = GuiService:GetGuiInset()

	mainFrame.InputChanged:Connect(function(inp)
		if inp.UserInputType == Enum.UserInputType.MouseMovement then
			local mainFrameAbs, pointerAbs = mainFrame.AbsoluteSize, pointer.AbsoluteSize
			pointer.Position = UDim2.fromOffset(
				math.min(inp.Position.X, mainFrameAbs.X - pointerAbs.X),
				math.min(inp.Position.Y + insetTop.Y, mainFrameAbs.Y - pointerAbs.Y)
			)
		end
	end)
end

if player.Character then --check if the character exists
	onCharacterAdded(player.Character)
end

player.CharacterAdded:Connect(onCharacterAdded)

surfaceGui = Path to your surfacegui (it should be in starter gui)
mainFrame = main frame of your gui (for getting dimensions)
pointer = object to move

This script goes in a LocalScript in starterplayerscripts

If you run into any iussues with this let me know!!!

Thank you guys!!!