-
What do you want to achieve?
A loop checks the mouse target, when it changes to a new object in a different model it executes a function. If the targeted object is in the same model it should do nothing. Needs to be part of the while true loop. It’s a local script if that matters.
-
What is the issue?
It doesn’t execute the function when a new mouse target is hovered without resetting other function.
-
What solutions have you tried so far?
Tried the only other post I could find on it, the one where someone needed the wall texture to update but couldn’t get it to work. Tried creating a LastPart variable and inserting it in different spots but no luck either.
UIS = game:GetService("UserInputService")
player = game:GetService("Players").LocalPlayer
mouse = player:GetMouse()
InRange = false
function RangeCheck()
if mouse.Target and player.Character.Humanoid.Health > 0 then
local magnitude = (mouse.Target.Position - player.Character.HumanoidRootPart.Position).Magnitude
if magnitude <= 4 then
InRange = true
elseif magnitude > 4 then
InRange = false
end
elseif mouse.Target == nil then
InRange = false
end
end
while true do
RangeCheck()
if InRange == true and player.Character.Humanoid.Health > 0 then
-- do stuff
elseif InRange == false or player.Character.Humanoid.Health <= 0 then
-- do stuff
end
wait(.1)
end
I am not entirely sure what you are trying to achieve but I believe you do need a lastPart variable.
Have you tried implementing it like this?
player = game:GetService("Players").LocalPlayer
mouse = player:GetMouse()
InRange = false
lastPart = nil
function RangeCheck()
if mouse.Target and mouse.Target ~= lastPart and player.Character.Humanoid.Health > 0 then
local magnitude = (mouse.Target.Position - player.Character.HumanoidRootPart.Position).Magnitude
if magnitude <= 4 then
InRange = true
lastPart = mouse.Target
elseif magnitude > 4 then
InRange = false
end
else
InRange = false
end
end
while true do
RangeCheck()
if InRange == true and player.Character.Humanoid.Health > 0 then
-- do stuff
elseif InRange == false or player.Character.Humanoid.Health <= 0 then
-- do stuff
end
wait(.1)
end
Didn’t work for my purposes but it did give me a eureka on how to modify it for what I needed. Thanks!
Edit: For anyone who stumbles upon this thread trying to resolve their own troubles, I created a function for interacting with said object and put a LastPart variable in there. Then added a check in the while true loop to see if the current mouse target was different the LastPart.
1 Like
I highly discourage this solution due to the staff saying numerous times how Mouse is a legacy feature and is going to be deprecated soon.
Instead, use UserInputService or ContextActionService .
New to scripting so pardon the ignorance but what would a better solution be? Raycasting using the 2D screen position to object?
Where would I look to find announcements such as mouse being deprecated? Gave a quick search just now and nothing explicit regarding it came back just that UIS or CAS should be used instead.
Well, it’s not deprecated now. But I wouldn’t be surprised since the staff keeps discouraging it to new developers.
At the very top of Mouse’s API documentation, it states:
Mouse has been superseded by UserInputService
and ContextActionService
, which cover a broader scope, are more feature rich, and support cross-platform patterns better. It remains supported because of its widespread use, but you should strongly consider using these alternatives.
So you would have to use either UserInputService or ContextActionService. Whichever does you fancy.
You’re more than welcome to keep using Mouse. But you should have a backup plan incase Roblox deprecates it and your script doesn’t break.