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)
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.
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.