I’ve been attempting to set a cursor image label inside a surfaceGUI adornee’d to a part relative to the player’s mouse position. I’ve looked for solutions, but they all have caused the same/new problem.
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")
local localPlayer = players.LocalPlayer
local mouse = localPlayer:GetMouse()
local currentCamera = workspace.CurrentCamera
local computerGui = script.Parent
local display = computerGui.Display
local wallpaper = display.Wallpaper
local cursor = wallpaper.Cursor
local function UpdateCursorPos()
local mouseTarget = mouse.Target
if mouseTarget == computerGui.Adornee then
local mousePos = userInputService:GetMouseLocation()
cursor.Position = UDim2.fromOffset(mouse.X - cursor.Position.X, mouse.Y - cursor.AbsolutePosition.Y)
mouse.Icon = "http://www.roblox.com/asset/?id=7649231250"
currentCamera.CameraType = Enum.CameraType.Scriptable
currentCamera.CFrame = workspace.ComputerCameraPart.CFrame
print(cursor.Position)
else
userInputService.MouseIconEnabled = true
currentCamera.CameraType = Enum.CameraType.Custom
end
end
runService.RenderStepped:Connect(UpdateCursorPos)
Unfortunately doesn’t work, as far as I’ve understood (I’m horrible at math) I should be on the right track with calculating the relative positions of X and Y with:
The fix was actually incredibly simple, I just used the MouseMoved event. Here’s the fully working code (without any of the debugs or unnecessary variables I forgot in it:
local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")
local localPlayer = players.LocalPlayer
local mouse = localPlayer:GetMouse()
local currentCamera = workspace.CurrentCamera
local computerGui = script.Parent
local display = computerGui.Display
local wallpaper = display.Wallpaper
local cursor = wallpaper.Cursor
local function UpdateCursorPos(X, Y)
local mouseTarget = mouse.Target
if mouseTarget == computerGui.Adornee then
cursor.Position = UDim2.fromOffset(X,Y)
currentCamera.CameraType = Enum.CameraType.Scriptable
currentCamera.CFrame = workspace.ComputerCameraPart.CFrame
userInputService.MouseIconEnabled = false
else
currentCamera.CameraType = Enum.CameraType.Custom
userInputService.MouseIconEnabled = true
end
end
display.MouseMoved:Connect(UpdateCursorPos)