InputChanged Check for mouse target delay

As the title says i’m using inputchanged to print the mouse.target name, however when i move around the player without moving the mouse it takes about 0,5 ~ seconds to start again his update frequence.

This would be not a problem normally but when i want a precise selection of a part when hovering with the mouse the selected part is usually different from the updated one

Video

Code

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local mouse = Player:GetMouse()
local highlight = Instance.new("Highlight")


UIS.InputChanged:Connect(function()
	if mouse.Target and mouse.Target.Name == "Selectable" then
		highlight.Parent = mouse.Target
	else
		highlight.Parent = game.ReplicatedStorage
	end
end)

mouse.Button1Down:Connect(function()
	if mouse.Target and mouse.Target.Name == "Selectable" then
		highlight.Parent = game.ReplicatedStorage
		mouse.Target:Destroy()
	end
end)
1 Like

Would connecting the function to RunService.Heartbeat in place of InputChanged solve the issue?

Is one of the options however running it for every frame(aprox. 60*sec) would impact the game performance, i’m pointing to have as much parts as possible on screen without performance problems or lag

I understand. Heartbeat functions aren’t the cause of a concern unless they do some heavy work. Your process isn’t very demanding, but the highlight reparenting part can be reduced to the times when it’s necessary, because changing Parent is among the slower operations compared to changing other properties.

New version (not tested)
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local Player = Players.LocalPlayer
local mouse = Player:GetMouse()
local highlight = Instance.new("Highlight")

local target = nil

RunService.Heartbeat:Connect(function()
	if mouse.Target == target then return end
	target = mouse.Target
	if target and target.Name == "Selectable" then
		highlight.Parent = target
	else
		highlight.Parent = ReplicatedStorage
	end
end)

UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		highlight.Parent = ReplicatedStorage
		target:Destroy()
	end
end)
1 Like

Also found this as option

		while UIS:IsKeyDown(input.KeyCode) do
			if input.KeyCode == Enum.KeyCode.W or  input.KeyCode == Enum.KeyCode.A or  input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.D then
				Hover_Update(mouse.Target)
			else
				Highlight.Parent = game.ReplicatedStorage
			end
			task.wait()
		end

But i think this could have the same performance impact as heartbeat, maybe even stronger.


Update:

If no duration is given, it will default to zero (0). This means the thread resumes on the very next step, which is equivalent in behavior to RunService.Heartbeat:Wait()

Suorce: Roblox documentation

I think one or the other may work since are equals

1 Like

That’s right, the performance impact is quite similar. task.wait() will let the code run as frequently as Heartbeat:Wait(). The IsKeyDown example doesn’t fit your code as much though, because it keeps looping until the given key is released.

So i should just use heartbeat to replace inputchanged and the button down section since are equals
This could save lines of code and with a if statement (active/unactive) would not impact at all when disabled

If I were you, I’d use the version (or a variation of the version) I sent in my second reply. It doesn’t differ much from your original; it includes one Heartbeat and one InputBegan connection. The script only updates the highlight’s Parent when the target changes. Anything beyond this is a micro-optimisation (such as replacement of UIS with CAS and a function bound solely to a specific key).

I replaced mouse.Button1Down with a UIS event afterwards.

1 Like

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