Will this script cause lag

So basically I have a script where whenever the player moves their mouse, it checks every player in the servers character and whether the users mouse is over their character or not. I’ve tested it out for a few minutes and there wasn’t really any lag but I just wanna make sure that over time it doesn’t start to lag.
Here is my script:

local mouse = game.Players.LocalPlayer:GetMouse()
local userInputService = game:GetService("UserInputService")
userInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
			if mouse.Target then
				for i,v in pairs(game.Players:GetChildren()) do
					if v.Character and mouse.Target:IsDescendantOf(v.Character) then
						highlight(v.Character)
						script.hover:Play()
						break
					end
				end
			end
	end
end)
2 Likes

I don’t think it will cause lag so.

With a small amount of player’s, minimal lag would occur. But as there are more players in the server, there are more players to loop through and in turn slowing down the loop.

Mouse.Target will always return something if the mouse is over a part. So, you don’t need the loop. All you would need to do is check if the part’s parent is a character. Then do whatever you’d like with it.

What is the highlight function?

This code should not lag your client. However, it will probably give some undesirable results. For instance, the hover sound will play repeatedly while the player moves their mouse. Secondly, the player mouse will not highlight characters who may be walking and become the mouse target. Because of those reasons, I recommend using :GetPropertyChangedSignal() method, connected to the Target property of Mouse. I also would recommend storing as a variables of the current character as to avoid repeating the hover sound while hovering over the same character but perhaps different parts.

Hope this helped you.

  • Galactiq
1 Like

It does this

1 Like

That is genius thank u bro I will do that right now

Can you show the function code?

Why would you like to see my code

To see if anything performance heavy is being done in them.

Haha its called “Tree House Story” by Mike Franklyn