If I wanted to add a selection box over whatever part the player’s mouse is over, am I stuck to a while loop using mouse.Hit or is there a cheaper way to do so?
.MouseEnter
fires whenever the mouse enters and hovers over a gui
How would I encapsulate the part in a GUI
wait I think I misinterpreted the question, if you meant when the mouse go over a workspace part then I think you can just use Mouse.Hit.Changed
instead of using a while loop
well what if there was a map with lots of different parts, wouldn’t that make it fire constantly, are there any optimizations?
it’s not as bad as a while loop tho
Just did some research on Mouse (havn’t really used it in depth since its kinda deprecated) but I think you can use Mouse.Target
and Mouse.TargetFilter
to further improve performance.
You could do something like this
local RunService = game:GetService'RunService'
local Players = game:GetService'Players'
local Mouse = Players.LocalPlayer:GetMouse()
local SelectionBox = Instance.new("SelectionBox", workspace)
local LastTarget
RunService.Heartbeat:Connect(function()
local CurrentTarget = Mouse.Target
if LastTarget ~= CurrentTarget then
LastTarget = CurrentTarget
SelectionBox.Adornee = CurrentTarget
end
end)
Also I’m not sure what Mouse.Hit.Changed
is since Hit
is just the CFrame
where the mouse is currently pointing to and CFrame
doesn’t have a Changed
event
I’m curious to the difference between run service and while loop. I haven’t looked too far into it but doesn’t RunService.Heartbeat just run every frame?
But you can get the vector3 position and do .Changed on that. Heartbeat runs every frame so it is even more expensive than a while loop
yes it does
Heartbeat runs every frame and a while loop runs while a condition is true. You’re still going to need some sort of yield inside of the while loop if you want to constantly check what part the mouse is hovering over, or else the script is gonna crash.
Also, I don’t recommend using Mouse.Move
because if you have moving parts inside your game and the mouse doesn’t move, then the selection box won’t be updated
actually yea what does he mean by mouse.Hit.Changed?