GetGuiObjectsAtPosition flickering

i have a custom cursor that i want to change when the player hovers over any button, and i am using this function PlayerGui:GetGuiObjectsAtPosition() to get the guiobjects at the mouse position
but i am running into an issue where the result is kind of flickering
heres a video of what i mean:

External Media

sometimes it detects a button and sometimes it doesnt, i have no idea why this is happening

this is the function i use to get the guiobjects, basically all it does is it filters out the cursor gui objects

function getGuiObjectsAtMousePos()
	local cursorPos = UIS:GetMouseLocation()
	local objects = plr.PlayerGui:GetGuiObjectsAtPosition(cursorPos.X, cursorPos.Y - getGuiInset())
	local filtered = {}
	for i, v in ipairs(objects) do
		if v:IsDescendantOf(cursor) or v == cursor then continue end
		filtered[i] = v
	end
	return filtered
end
full code here
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local RUN = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local GUI = game:GetService("GuiService")

local cursor = script.Parent
local screenGUI = cursor.Parent
screenGUI.Enabled = true
UIS.MouseIconEnabled = false

local cursorIcon = "idle"

function getGuiInset()
	return table.pack(GUI:GetGuiInset())[1].Y
end

function getGuiObjectsAtMousePos()
	local cursorPos = UIS:GetMouseLocation()
	local objects = plr.PlayerGui:GetGuiObjectsAtPosition(cursorPos.X, cursorPos.Y - getGuiInset())
	local filtered = {}
	for i, v in ipairs(objects) do
		if v:IsDescendantOf(cursor) or v == cursor then continue end
		filtered[i] = v
	end
	return filtered
end

function switchTooltipType(ttType)
	if ttType == "General" then
		cursor.Tooltips.PickUp.Visible = false
		cursor.Tooltips.GeneralTooltip.Visible = true
	elseif ttType == "PickUp" then
		cursor.Tooltips.PickUp.Visible = true
		cursor.Tooltips.GeneralTooltip.Visible = false
	elseif ttType == "None" then
		cursor.Tooltips.PickUp.Visible = false
		cursor.Tooltips.GeneralTooltip.Visible = false
	end
end

cursor.Tooltips.GeneralTooltip:GetPropertyChangedSignal("Visible"):Connect(function()
	local new = cursor.Tooltips.GeneralTooltip.Visible
	if new then
		cursor.Tooltips.GeneralTooltip.TextTransparency = 1
		cursor.Tooltips.GeneralTooltip.UIStroke.Transparency = 1
		cursor.Tooltips.GeneralTooltip.Position = UDim2.new(1.1,0,1.1,8)
		TS:Create(cursor.Tooltips.GeneralTooltip, TweenInfo.new(0.4), {TextTransparency = 0, Position = UDim2.new(1.1,0,1.1,0)}):Play()
		TS:Create(cursor.Tooltips.GeneralTooltip.UIStroke, TweenInfo.new(0.4), {Transparency = 0.79}):Play()
	end
end)

cursor.Tooltips.PickUp:GetPropertyChangedSignal("Visible"):Connect(function()
	local new = cursor.Tooltips.PickUp.Visible
	if new then
		cursor.Tooltips.PickUp.ObjectText.TextTransparency, cursor.Tooltips.PickUp.ActionText.TextTransparency = 1, 1
		cursor.Tooltips.PickUp.ObjectText.UIStroke.Transparency, cursor.Tooltips.PickUp.ActionText.UIStroke.Transparency = 1, 1
		cursor.Tooltips.PickUp.ObjectText.Position, cursor.Tooltips.PickUp.ActionText.Position = UDim2.new(1.1,0,1.1,24), UDim2.new(1.1,0,1.1,8)
		TS:Create(cursor.Tooltips.PickUp.ObjectText, TweenInfo.new(0.4), {TextTransparency = 0, Position = UDim2.new(1.1,0,1.1,16)}):Play()
		TS:Create(cursor.Tooltips.PickUp.ActionText, TweenInfo.new(0.4), {TextTransparency = 0, Position = UDim2.new(1.1,0,1.1,0)}):Play()
		TS:Create(cursor.Tooltips.PickUp.ObjectText.UIStroke, TweenInfo.new(0.4), {Transparency = 0.79}):Play()
		TS:Create(cursor.Tooltips.PickUp.ActionText.UIStroke, TweenInfo.new(0.4), {Transparency = 0.79}):Play()
	end
end)

cursor.Tooltips.PickUp.ObjectText:GetPropertyChangedSignal("ContentText"):Connect(function()
	cursor.Tooltips.PickUp.ObjectText.MaxVisibleGraphemes = 0
	TS:Create(cursor.Tooltips.PickUp.ObjectText, TweenInfo.new(0.4), {MaxVisibleGraphemes = cursor.Tooltips.PickUp.ObjectText.ContentText:len()}):Play()
end)

cursor.Tooltips.GeneralTooltip:GetPropertyChangedSignal("ContentText"):Connect(function()
	cursor.Tooltips.GeneralTooltip.MaxVisibleGraphemes = 0
	TS:Create(cursor.Tooltips.GeneralTooltip, TweenInfo.new(0.4), {MaxVisibleGraphemes = cursor.Tooltips.GeneralTooltip.ContentText:len()}):Play()
end)

cursor.CursorImage:GetPropertyChangedSignal("Visible"):Connect(function()
	local new = cursor.CursorImage.Visible
	cursor.CursorCircle.Visible = not new
end)

RUN.RenderStepped:Connect(function()
	-- STUFF
	mouse.TargetFilter = plr.Character
	local cursorHit = mouse.Target
	
	-- SET CURSOR POS
	local cursorPosPx = UIS:GetMouseLocation()
	local cursorPosScaleX, cursorPosScaleY = cursorPosPx.X / screenGUI.AbsoluteSize.X, cursorPosPx.Y / screenGUI.AbsoluteSize.Y
	cursor.Position = UDim2.fromScale(cursorPosScaleX, cursorPosScaleY)
	
	-- TOOLTIP
	if cursorHit then
		if cursorHit.Parent:HasTag("CC_Tooltip") then
			if cursorHit:HasTag("CC_Tooltip_Regular") or cursorHit.Parent:HasTag("CC_Tooltip_Regular") then
				switchTooltipType("General")
				local tooltipText = cursorHit:GetAttribute("CCTooltipText") or cursorHit.Parent:GetAttribute("CCTooltipText")
				cursor.Tooltips.GeneralTooltip.Text = tooltipText
				cursor.CursorImage.Visible = false
			end
		elseif cursorHit.Parent:HasTag("Item") then
			switchTooltipType("PickUp")
			local tooltipText = cursorHit.Parent:GetAttribute("ItemDisplayName") or cursorHit.Parent:GetAttribute("ItemModelName") or "Couldnt find name because the scripter is stoopid"
			cursor.Tooltips.PickUp.ObjectText.Text = tooltipText
			cursor.CursorImage.Visible = true
			cursor.CursorImage.Image = "rbxassetid://95244368038569"
		else
			switchTooltipType("None")
			cursor.CursorImage.Visible = false
		end
	else
		switchTooltipType("None")
		cursor.CursorImage.Visible = false
	end
	
	-- SET CURSOR ICON
	cursorIcon = "idle"
	
	local guiObjectsAtMousePos = getGuiObjectsAtMousePos()
	print(guiObjectsAtMousePos[1])
	if guiObjectsAtMousePos[1] then
		if guiObjectsAtMousePos[1]:IsA("TextButton") or guiObjectsAtMousePos[1]:IsA("ImageButton") then
			cursorIcon = "interact"
		end
	end
	
	if plr:GetAttribute("HeldItem") then
		cursorIcon = "grabbed"
	elseif plr:GetAttribute("HoveringOverItem") and cursorIcon ~= "interact" then
		cursorIcon = "grab"
	end
	
	if cursorIcon == "idle" then
		cursor.CursorImage.Visible = false
	elseif cursorIcon == "grab" then
		cursor.CursorImage.Visible = true
		cursor.CursorImage.Image = "rbxassetid://74208876829860"
	elseif cursorIcon == "grabbed" then
		cursor.CursorImage.Visible = true
		cursor.CursorImage.Image = "rbxassetid://128395526828668"
	elseif cursorIcon == "interact" then
		cursor.CursorImage.Visible = true
		cursor.CursorImage.Image = "rbxassetid://133197009553044"
	end
end)

i switched the getGuiObjectsAtMousePos function to use table.insert instead and it fixed somehow

function getGuiObjectsAtMousePos()
	local cursorPos = UIS:GetMouseLocation()
	local objects = plr.PlayerGui:GetGuiObjectsAtPosition(cursorPos.X, cursorPos.Y - getGuiInset())
	local filtered = {}
	for i, v in ipairs(objects) do
		if v:IsDescendantOf(cursor) or v == cursor then continue end
		table.insert(filtered, v)
	end
	return filtered
end

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